收录日期:2021/01/26 17:44:21 时间:2010-04-03 04:03:55 标签:asp.net-mvc-2,viewmodel,multiple-forms
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.
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.
Rob
Using ViewModels in ASP.NET MVC 2 - multiple forms
Validation on ViewModels in ASP.NET MVC
Hydrating ViewModels in ASP.NET MVC
Multiple forms in ASP.NET MVC
Using HTML forms in ASP.NET MVC?
ASP.NET MVC: using EF entities as viewmodels? [duplicate]
Question about ASP.NET MVC 2 Custom ViewModels
ASP .NET MVC 2 - ViewModels vs regular Views and Models
Persisting data from multiple forms in ASP.NET MVC
How to get ID of EditorFor with nested viewmodels in asp.net mvc 2
ASP.NET MVC 2 Multiple PartialView Forms fieldname conflict
ASP.NET MVC: convention for organizing ViewModels
ASP.Net MVC Architecture - Location of ViewModels
ASP.NET MVC - ViewModels For Edit
MVC asp.net: Submit multiple forms
multiple forms in asp.net
Multiple Forms in ASP.NET
Upload File Validation using jQuery Forms in ASP.NET MVC