ASP.NET MVC 2 - 视图模型$ P ​​$ PFIX [英] ASP.NET MVC 2 - ViewModel Prefix

查看:145
本文介绍了ASP.NET MVC 2 - 视图模型$ P ​​$ PFIX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的关联不同的模型视图中使用的RenderPartial两次。问题是,一些属性是在这两种模式(昵称,密码)present。他们没有preFIX,所以即使是ID或名字都在输出相同。现在,如果我有一个昵称或密码模型误差,这两个领域得到强调。

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 prefix, so even the id's or names are equal in the output. Now, if I have model errors for nickname or password, both fields get highlighted.

主视图:

<div>
    <% Html.RenderPartial("Register", Model.RegisterModel); %>
</div>
<div>
    <% Html.RenderPartial("Login", Model.LoginModel); %>
</div>

登录PartialView:

Login PartialView:

<% using (Html.BeginForm("Login", "Member")) { %>
<fieldset>
    <legend>Login</legend>
    <p>
        <%= Html.LabelFor(x => x.Nickname) %>
        <%= Html.TextBoxFor(x => x.Nickname) %>
    </p>
    <p>
        <%= Html.LabelFor(x => x.Password) %>
        <%= Html.PasswordFor(x => x.Password) %>
    </p>    
    <input type="submit" value="Login" />
</fieldset>
<% } %>

注册PartialView:

Register PartialView:

<% using (Html.BeginForm("Register", "Member")) { %>
<fieldset>
    <legend>Register</legend>
    <p>
        <%= Html.LabelFor(x => x.Nickname) %>
        <%= Html.TextBoxFor(x => x.Nickname) %>
    </p>
    <p>
        <%= Html.LabelFor(x => x.Email) %>
        <%= Html.TextBoxFor(x => x.Email) %>
    </p>
    <p>
        <%= Html.LabelFor(x => x.Password) %>
        <%= Html.PasswordFor(x => x.Password) %>
    </p>
    <p>
        <%= Html.LabelFor(x => x.PasswordRepeat) %>
        <%= Html.PasswordFor(x => x.PasswordRepeat) %>
    </p>
    <input type="submit" value="Register" />
</fieldset>
<% } %>

我怎样才能改变呢?

How can I change this?

推荐答案

Html.RenderPartial 你可以使用<一个href=\"http://weblogs.asp.net/scottgu/archive/2009/07/31/asp-net-mvc-v2-$p$pview-1-released.aspx\">editor模板它将处理prefixes。

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

因此​​,在你的主视图:

So in your main view:

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

然后创建一个文件夹查看/共享/ EditorTemplates / RegisterModel.ascx (该文件的名称中使用的 EditorFor helper方法)。另请注意,这部分应该是强类型的 RegisterModel 属性的类型:

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<Ns.Models.RegisterModel>" %>

<% using (Html.BeginForm("Register", "Member")) { %>
<fieldset>
    <legend>Register</legend>
    <p>
        <%= Html.LabelFor(x => x.Nickname) %>
        <%= Html.TextBoxFor(x => x.Nickname) %>
    </p>
    <p>
        <%= Html.LabelFor(x => x.Email) %>
        <%= Html.TextBoxFor(x => x.Email) %>
    </p>
    <p>
        <%= Html.LabelFor(x => x.Password) %>
        <%= Html.PasswordFor(x => x.Password) %>
    </p>
    <p>
        <%= Html.LabelFor(x => x.PasswordRepeat) %>
        <%= Html.PasswordFor(x => x.PasswordRepeat) %>
    </p>
    <input type="submit" value="Register" />
</fieldset>
<% } %>

您可以在定义的登录模式不同的部分查看/共享/ EditorTemplates / LoginModel.ascx

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

这篇关于ASP.NET MVC 2 - 视图模型$ P ​​$ PFIX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆