1

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.