2

I couldn't find any documentation around using multiple forms in an ASP.NET MVC 2 ViewModel approach.

i.e. In the built in application when you select New MVC2 web app, the register page uses a ViewPage which inherits like this:

Inherits="System.Web.Mvc.ViewPage<rs30UserWeb.Models.RegisterModel>"

I wanted to use that approach on a page with multiple forms, but that RegisterModel only supported one form.

3
  • Can you be a little bit more specific of why you need multiple ViewModels? There's a chance the new EditorFor is a more appropriate direction to go in? stackoverflow.com/questions/1235646/… Commented Mar 31, 2010 at 4:40
  • I have multiple forms on a single page. I'm using the ASP.NET MVC2 model validation approach. Commented Mar 31, 2010 at 4:42
  • That first link I added wasn't particularly helpful, this is better blogs.msdn.com/nunos/archive/2010/02/08/… Commented Mar 31, 2010 at 4:49

1 Answer 1

5

The approach is to use a composite viewmodel:

namespace MyApp.Models
{
    public class MyCompositePageModel
    {
        public RegisterModel registerModel;
        public LoginModel loginModel;
    }
}

When you do that, inherit the View from it:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<rs30UserWeb.Models.RegisterModel>" %>

Then reference the individual models in the page:

<%= Html.TextBoxFor(m => m.loginModel.Email) %>
<%= Html.ValidationMessageFor(m => m.loginModel.Email) %>

Hope someone else finds this useful.

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.