使用实体框架保留模型绑定对象

发布于 2024-08-03 14:19:18 字数 593 浏览 2 评论 0原文

我会尽量保持简短。

我的控制器在这里...

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(CustomObject myCustomObject)
{
     ...
}

myCustomObject 看起来很棒。但是,如果我想使用实体框架保存它,我需要做这样的事情......

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(CustomObject myCustomObject)
{
     CustomObject existingObject = repository.GetCustomObject(myCustomObject.ID);

     // Set all the attributes of myCustomObject to existingObject
     existingObject.SomeMapperFunction(myCustomObject)

     repository.Save();
}

有没有办法可以避免进行这种映射练习?

I'll try to keep this short and concise.

I got my controller here...

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(CustomObject myCustomObject)
{
     ...
}

Where myCustomObject looks great. But, if I want to save this using the entity framework, I need to do something like this...

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(CustomObject myCustomObject)
{
     CustomObject existingObject = repository.GetCustomObject(myCustomObject.ID);

     // Set all the attributes of myCustomObject to existingObject
     existingObject.SomeMapperFunction(myCustomObject)

     repository.Save();
}

Is there a way I can keep from doing this mapping excersise?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id)
{
     CustomObject existingObject = repository.GetCustomObject(id);

     TryUpdateModel(existingObject);
     // You additionaly can check the ModelState.IsValid here

     repository.Save();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id)
{
     CustomObject existingObject = repository.GetCustomObject(id);

     TryUpdateModel(existingObject);
     // You additionaly can check the ModelState.IsValid here

     repository.Save();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文