views:

46

answers:

2

Hi,

one thing that has been puzzling me since learning MVC2 is the following case scenario:

I have a view which contains two latest news lists, a login form and a signup form.

Every example I found on Views and View Models so far has a one-to-one example such as a simple login form etc. But how do I create a model that provides the properties and validation for a login and signup form and manages the data for the news lists.

Can I pass multiple models in the strongly typed view?

When I created one model the form validation would fail as it expects all fields - login and signup to be filled.

I am missing some advanced examples or information. Any help is appreciated.

A: 

I usualy create .ascx in this case.

I make ascx strongly typed for the model (in your case LoginModel) I make a second ascx strongly typed (in your case SignupModel).

Then I make aspx and put those 2 ascx-es inside of it <% Html.RenderPartial("Login", Model.Login); %> and similar for the other one.

And you make aspx also stronly typed as

class PageModel
{
   public LoginModel Login { get; set; }
   public SignupModel Signup { get; set; }
}
dmonlord
A: 

Hi,

thanks - so basically what you are saying is that for the rendering of the forms/elements you would use controls and create a page model consisting of two other models.

Do I need to use the controls to render the elements? To me this solution appears to be less MVC like as it does not really allow abstraction of the View.

I will try this and post the result.

Thanks

Stefan
Yes, I would use controls (to simplify syntax - Model.Property against Model.Property1.Property2).You don't need to use controls to render elements, but it would be simpler, but as I was saying, it will be simpler that way. And it's not less MVC like. You still pass a model to the view, it's just a little bit of a different model.
dmonlord