ASP.NET MVC许多对客户端多模 [英] ASP.NET MVC many to many model on client side

查看:81
本文介绍了ASP.NET MVC许多对客户端多模的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个多对多表:用户 - < &的UserRole GT; - 角色。设置我的模式是这样的:

I have 3 many-many tables: Users -< UserRoles >- Roles. I set up my model like this:

public class User
{
    public int UserId {get; set;};
    public IEnumerable<Role> Roles {get; set;};
}

public class Role
{
    public int RoleId {get; set;};
    public string RoleName {get; set};
}

public class UserDisplayModel
{
    public User User{get; set;};
    public IEnumerable<Role> AllRoles {get; set;}
}

在编辑/创建用户,我怎么去获得所扮演的角色选中的复选框中的控制器和我怎么会在我看来设置呢?

When editing/creating the user, how do I go about getting the checked checkbox of the roles in the controller and how would I set this up in my view?

如果我是在路上设置我的模型开始就错了,请告诉我,并帮助我如何会去这样做。

If i'm wrong from the start on the way I set up my Model, please tell me and assist on how I would go about doing this.

感谢。

推荐答案

关键是,你需要你的收藏在视图中正确呈现。首先,一个布尔属性添加到角色视图数据对象,所以我们有东西给我们的复选框绑定到:

The key is that you need your collection properly rendering in the view. First off, add a Boolean property to the Role view data object so we have something to bind our check box to:

public class Role
{
    public bool IsInRole { get; set; }
    [HiddenInput(DisplayValue = false)]
    public int RoleId { get; set; }
    [HiddenInput(DisplayValue = true)]
    public string RoleName { get; set; }
}

请注意,我把一些HiddenInput属性的属性(稍后更多)。你也可以通过你的用户对象如上图所示的观点 - 这已经有角色的子集。有几个方法可以使这个集合中的看法,但最简单的一种是:

Notice I put some HiddenInput attribute on the properties (more on that later). Also you could pass your User object as shown above to the view - this already has the child collection of Roles. There are a few ways to render this collection in the view, but one of the easiest is:

<%: Html.EditorFor(m => m.Roles) %>

现在让上面的行做我们想要的添加对角色对象的编辑模板。添加到Role.ascx /查看/共享/ EditorTemplates文件夹中。 Roles.ascx可以看起来像这样:

Now add an editor template for the Role object so the line above does what we want. Add Role.ascx to /Views/Shared/EditorTemplates folder. Roles.ascx can looking something like this:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MvcApplication5.Controllers.Role>" %>
<%: Html.EditorFor(m => m.IsInRole) %>
<%: Html.EditorFor(m => m.RoleId) %>
<%: Html.EditorFor(m => m.RoleName) %>

您会看到当你做你的HTML看起来像这样一个观点来源:

You'll see when you do a view source that your html looks something like this:

<input class="check-box" id="Roles_0__IsInRole" name="Roles[0].IsInRole" type="checkbox" value="true" /><input name="Roles[0].IsInRole" type="hidden" value="false" />
<input id="Roles_0__RoleId" name="Roles[0].RoleId" type="hidden" value="1" />
RoleName1<input id="Roles_0__RoleName" name="Roles[0].RoleName" type="hidden" value="RoleName1" />
<input class="check-box" id="Roles_1__IsInRole" name="Roles[1].IsInRole" type="checkbox" value="true" /><input name="Roles[1].IsInRole" type="hidden" value="false" />
<input id="Roles_1__RoleId" name="Roles[1].RoleId" type="hidden" value="2" />
RoleName2<input id="Roles_1__RoleName" name="Roles[1].RoleName" type="hidden" value="RoleName2" />

这是模型时,你的窗体回约束力的关键。我们使用DisplayValue = TRUE的显示名,因为我们需要回发模型绑定隐藏的输入,但它必须是只读的。对于角色ID,这是一个隐藏的输入和没有值显示给用户。见<一href=\"http://geekswithblogs.net/michelotti/archive/2010/06/14/a-closer-look-at-the-hiddeninput-attribute-in-mvc-2.aspx\"相对=nofollow>这个帖子关于HiddenInput更多信息。

This is key for model binding when your form is posted back. We used DisplayValue=true for the display name because we need the hidden input for post back model binding, but it's need to be read-only. for the roleId, that is a hidden input and no value is displayed to the user. See this post for more information on the HiddenInput.

在发布此回,MVC内置的模型绑定将确保您的角色集合构造属性,您将看到的复选框状态适当地反映在模型中。

When you post this back, MVC built-in model binder will ensure that your roles collection is constructed property and you'll see the check boxes state properly reflected in your model.

这篇关于ASP.NET MVC许多对客户端多模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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