ASP.NET MVC 2 - ViewModel Prefix

前端 未结 4 1688
栀梦
栀梦 2021-02-13 21:45

I want to use RenderPartial twice in my view with different models associated. The problem is that some properties are present in both models (nickname, password). They have no

4条回答
  •  一个人的身影
    2021-02-13 21:58

    Instead of using Html.RenderPartial you could use editor templates which will handle prefixes.

    So in your main view:

    <%-- See below what does the second argument mean --%> <%= Html.EditorFor(x => x.RegisterModel, "RegisterModel") %>
    <%= Html.EditorFor(x => x.LoginModel, "LoginModel") %>

    And then create a folder Views/Shared/EditorTemplates/RegisterModel.ascx (The name of this file is used in the EditorFor helper method). Also notice that this partial should be strongly typed to the type of the RegisterModel property:

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
    
    <% using (Html.BeginForm("Register", "Member")) { %>
    
    Register

    <%= Html.LabelFor(x => x.Nickname) %> <%= Html.TextBoxFor(x => x.Nickname) %>

    <%= Html.LabelFor(x => x.Email) %> <%= Html.TextBoxFor(x => x.Email) %>

    <%= Html.LabelFor(x => x.Password) %> <%= Html.PasswordFor(x => x.Password) %>

    <%= Html.LabelFor(x => x.PasswordRepeat) %> <%= Html.PasswordFor(x => x.PasswordRepeat) %>

    <% } %>

    You could define a different partial for the login model in Views/Shared/EditorTemplates/LoginModel.ascx

提交回复
热议问题