开发者

ASP.NET MVC Child ViewModel validation

Working with ViewModels, I would like to split them:

public SignUpViewModel //for display
{
   public SignUpUserViewModel SignUpUserViewModel { get; set; } //for validation
   public IEnumerable<SelectListItem> UserTypes {get;set;}
}

So I want to render SignUpViewModel but get SignUpUserViewModel as an argument of POST-action.

Do you find this reasonable? What are开发者_开发百科 the ways to implement this approach?

Looks like DefaultModelBinder doesn't work this way: it doens't understand SignUpUserViewModel is a property of SignUpViewModel. So one way I see is to implement custom model binder. Any other?


I think that's reasonable. Just have your post action bind to the SignUpUserViewModel.

E.g.

[HttpPost]
public ActionResult Edit(int id, SignUpUserViewModel editForm)

On a side note, looking at your SignUpViewModel vs SignUpUserViewModel, I think you could just combine them into the one view model.

In saying that I will say that I too sometimes have a similar setup to what you have, e.g. ViewModel and a child FormModel (posting and binding to the FormModel) but I put anything to do with the form like validation and the SelectListItems in the FormModel. So in your case above, I would just combine them into the one FormModel.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜