如何一个asp.net MVC模式生成输入的名字呢? [英] How does an asp.net MVC model generate input names?

查看:98
本文介绍了如何一个asp.net MVC模式生成输入的名字呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MVC中的EditorFor观点与剃刀标记,如果你这样做:

In an EditorFor view in MVC with Razor markup, if you do this:

@Html.TextBox("")

中的HTML出来用正确的输入名称为模型,就像这样:

The HTML comes out with the correct input name for the model, like so:

<input id="usr_Roles" name="usr.Roles" type="text" value="">

在usr.Roles的名字来源于此EditorFor行:

The "usr.Roles" name comes from this EditorFor line:

@Html.EditorFor(modelItem => usr.Roles, "RoleList") 

不幸的是,我试图生成一组名为usr.Roles复选框。因为MVC方式呈现复选框,我不能使用Html.Checkbox。我可以手动生成它们像这样:

Unfortunately, I'm trying to generate a set of checkboxes with the name "usr.Roles". Because of the way MVC renders checkboxes, I can't use Html.Checkbox. I can generate them manually like so:

@foreach (string roleName in Roles.GetAllRoles())
{
    string checkboxId = "checkbox_" + roleName;
    <input type="checkbox" name="usr.Roles" id="@checkboxId" value="@roleName" @Html.Raw(Model.Contains(roleName) ? "checked" : "") />
    <label for="@checkboxId">@roleName</label>
} 

以上的作品,但只是因为我手动指定usr.Roles作为复选框的名称。

The above works, but only because I manually specify "usr.Roles" as the checkbox name.

我的问题是:凡在对象和属性的迷宫请问@ Html.TextBox对象找到usr.Roles,USR-角色字符串?而且我能找到它自己,并在复选框使用它?

My question is this: Where in the maze of objects and properties does the @Html.TextBox object find the "usr.Roles" "usr-Roles" strings? And can I find it myself and use it in the checkboxes?

推荐答案

我不知道为什么在地球上,你会想手动产生复选框id和名称,而不是简单地使用 HTML .CheckBoxFor 帮助它做这一切给你,但如果你有一定的原因,这里的如何:

I don't know why on earth you would be wanting to manually generate ids and names of your checkboxes instead of simply using the Html.CheckBoxFor helper which does all this for you, but in case you have some reasons, here's how:

@{
    var name = ViewData.TemplateInfo.GetFullHtmlFieldName("");
}

@foreach (string roleName in Roles.GetAllRoles())
{
    string checkboxId = "checkbox_" + roleName;
    <input type="checkbox" name="@name" id="@checkboxId" value="@roleName" @Html.Raw(Model.Contains(roleName) ? "checked" : "") />
    <label for="@checkboxId">@roleName</label>
} 

显然,这显示在这里纯粹一些教育目的。我真的会避免这样的怪物,并简单地用HTML辅助和编辑模板。这让我震惊在您看来的另一件事是code的下面一行: Roles.GetAllRoles()。这是因为如果您的视图查询数据。这是最后一件事,一个观点应该做的。它是控制器的责任来查询数据,填充视图模型,并通过这个视图模型视图,以便它只是显示的数据。

Obviously that's shown here pure for some educational purposes. I would really avoid such monstrosity and simply use Html helpers and editor templates. Another thing that shocks me in your view is the following line of code: Roles.GetAllRoles(). It's as if your view is querying data. That's last thing a view should do. It's the controller's responsibility to query data, fill a view model and pass this view model to the view so that it simply shows the data.

因此​​,让我们试着来阐述你的榜样。从我能理解你正在试图表明一个复选框列表为每个角色,使用户可以选择他们。

So let's try to elaborate your example. From what I can understand you are trying to show a list of checkboxes for each role so that the user can select them.

往常一样,你开始一个视图模型前pressing你的意见要求:

As always you start with a view model expressing your views requirements:

public class MyViewModel
{
    public IEnumerable<RoleViewModel> Roles { get; set; }
}

public class RoleViewModel
{
    public string RoleName { get; set; }
    public bool Selected { get; set; }
}

那么你就写一个控制器的动作,将查询数据并填写视图模型:

then you write a controller action which will query your data and fill the view model:

public ActionResult Index()
{
    var roles = Roles.GetAllRoles();
    var model = new MyViewModel
    {
        Roles = roles.Select(role => new RoleViewModel
        {
            RoleName = role,
            Selected = ??????
        })
    };
    return View(model);
}

那么你将有一个 Index.cshtml 查看:

@model MyViewModel
@Html.EditorFor(x => x.Roles)

和将要呈现的角色集合中的每个元素的相应的编辑器模板(〜/查看/共享/ EditorTemplates / RoleViewModel.cshtml

and a corresponding editor template which will be rendered for each element of the Roles collection (~/View/Shared/EditorTemplates/RoleViewModel.cshtml):

@model RoleViewModel
@Html.CheckBoxFor(x => x.Selected)
@Html.LabelFor(x => x.Selected, Model.RoleName)

没有更多的的foreach 循环中的观点,没有更多的丑陋黑客与名称和ID,没有更多的数据视图拉。

No more foreach loops in the views, no more ugly hacks with names and ids, no more data pulling from the views.

这篇关于如何一个asp.net MVC模式生成输入的名字呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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