绑定到ViewModel时如何更新Model? [英] How to update Model when binding to a ViewModel?

查看:443
本文介绍了绑定到ViewModel时如何更新Model?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个[HttpPost]动作方法签名,如下所示:

I have an [HttpPost] action method signature like this:

[HttpPost]
public ActionResult Edit(ExistingPostViewModel model)
{
   // Save the edited Post.
}

现在,在过去(当我不使用ViewModels时,例如R& D),我实现了这样的 Edit 方法的实现:

Now, in the past (when i didn't use ViewModels, e.g R&D), i had an implementation of an Edit method like this:

[HttpPost]
public ActionResult Edit(Post model)
{
    var existingPost = repo.Find(model.Id);
    TryUpdateModel(existingPost);
    repo.Save(existingPost);  
    return RedirectToAction("Success", existingPost.Id);
}

哪个做得很棒.

但是我很困惑如何使以上内容适应ViewModel方法.

But i'm confused how to adapt the above to the ViewModel approach.

如果我这样做:

TryUpdateModel(existingPost)

使用我的ViewModel方法,发生的事情很少.没有错误,但是没有任何更新,因为MVC不知道如何从ExistingPostViewModel(在Post-> Post之前)更新Post.

With my ViewModel approach, not much happens. No errors, but nothing is being updated because MVC won't know how to update a Post from a ExistingPostViewModel (before it was Post -> Post).

现在,我正在使用AutoMapper.所以我想我可以从ViewModel映射到Post,然后保存帖子.

Now, i'm using AutoMapper. So i thought i could map from the ViewModel to the Post, then save the post.

但是我基本上是覆盖了一切.我不想这样做,并打败了削减ViewModel的观点.

But then im basically overriding everything. Which i don't want to do and defeats the point of the cut down ViewModel.

任何人都可以使我困惑吗?

Can anyone un-confuse me?

这似乎是一个非常普遍的情况,对于人们如何解决这个问题我深感困惑.我只能看到3种可能的解决方案:

This seems like a really common scenario, and i am totally stumped as to how people solve this. I can only see 3 possible solutions:

  1. 不要在HTTP POST中使用ViewModel.就像我说的那样,过去我曾为R& D进行过此操作,但它确实有效,但是现在我看到了View的发展方式(验证,简单性),我不能为了这个问题而妥协.

  1. Don't use a ViewModel in the HTTP POST. As i said i did this in the past for R&D and it works, but now i see how my View's have evolved (validation, simplicity), and i can't compromise that just for the sake of this problem.

不要使用TryUpdateModel.可能,但是那我将如何合并更改?

Don't use TryUpdateModel. Possibly, but then how would i merge in the changes?

从左到右使用.啊.但目前看来,这似乎是我的追求方式.

Use left-to-right. Ugh. But at the moment this seems to be the way im leaning.

有人请给我解决方案4! :)

Someone please give me solution #4! :)

顺便说一句,我正在使用ASP.NET MVC 3,Razor和实体框架.

BTW, i'm using ASP.NET MVC 3, Razor and Entity Framework.

推荐答案

我实际上在当前正在研究的项目中遇到了这个完全相同的问题.尽管我不喜欢它,但最终还是采取了从左到右的方法,并将视图模型数据手动映射回我的实体.

I actually ran into this exact same issue in a project I'm currently working on. As much as I wasn't a fan of it, I ended up doing the left to right approach and manually mapping my viewmodel data back to my entity.

此方法的唯一好处是,它确实可以为您提供更多控制权.自从我开始使用更多的复合视图模型(实际上,您在视图模型中拥有多个实体的字段)以来,以这种方式进行操作就变得更加有意义.

The only nice thing about this approach is it does give you more control. Since I started using more compound viewmodels, where you actually have fields from more than one entity in your viewmodel, it started making more sense to do things this way.

我也在使用AutoMapper,你是完全正确的,当您尝试执行简单的更新操作时,它确实变得笨拙.希望我能为您提供一些超级聪明的解决方法,但是老式方式"似乎可以使我所做的工作最好地完成.

I'm also using AutoMapper, and you're absolutely right, it does get awkward when you're trying to do a simple update operation. Wish I had some super clever workaround for you, but the "old fashioned way" seems to get the job done best for the work I've been doing.

这篇关于绑定到ViewModel时如何更新Model?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆