2

I need to capture three different types of information from a new user who is registering for the first time in an MVC 3 app (using EF code first). Ideally on the one page (which will have three tabs)

  1. User info
  2. Extended user info
  3. Benefits chosen by user

The register method of the account controller should populate the user table when it fires. I need some advice on how best to capture the other data. DO I create a UserDetails model for the additional data? If so is it possible to update this from the same page? (which would be the Account/Register page. Do I need to do something in the account controller or will the relationship between the models be enough?

Are there any good examples about that would explain this? I tried the MVC Contoso University one but couldn't see if I could do this.

Any advice very welcome :)

Thanks

2 Answers 2

2

You can create a view model that combines all the fields you need from the other models, and strongly type your view to that. Once the user submits the info back to the controller, you'll process each property appropriately.

public class RegisterViewModel()
{
  //Userinfo
  public string UserName {get; set;}
  ...
  //Extended user info
  public string FirstName {get; set;}
  public string LastName{get; set;}
  ...
  //Benefits
  public string BenefitName {get; set;}
  ...
}

and then

[HttpPost]
public ActionResult Register (RegisterViewModel viewModel)
{
  //grab the user info from the view model and process it
  viewModel.UserName...

  //grab the extended info and process it
  viewModel.FirstName...
  //grab the benefit info and process it
  viewModel.BenefitName...
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help. I tried what you suggested but still get a similar error when trying to register - Object reference not set to an instance of an object. - newuser.UserDetails.Firstname = viewModel.Firstname;
Ahh, I hadn't created an instance of NewUser. Done that now and all working . Than ks
0

A single view can only have one model. You can put all the information you need there. It's the simplest solution. The model of your view doesn't have to map to classes you're using elsewhere so you can have your User info, Extended user info and Benefits chosen by user all in one model.

Alternatively, you could make this a two- or three-step process, but you said you don't want that.

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.