【问题标题】:Razor LabelFor and DropdownForRazor LabelFor 和 DropdownFor
【发布时间】:2014-07-22 23:16:31
【问题描述】:

我正在使用带有 EF6 的 MVC5。我需要在一个视图中创建几十个下拉列表,其中包含用户可选择的首选项,然后在整个 Web 应用程序中使用这些首选项。

我遇到的问题是剃刀视图中的 LabelFor 和 DropdownListFor 行都有错误:

编译器错误消息:CS0411:无法从中推断方法“System.Web.Mvc.Html.LabelExtensions.LabelFor System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>, string)”的类型参数用法。尝试明确指定类型参数。

我已经跟踪了我的代码以确保Model.Preferences 包含我想要的数据,并且确实如此。实际上,它与Html.LabelHtml.DropdownList 一起工作得很好,但我无法让所选项目以这种方式工作,所以我切换到DropdownListFor,因为我发现在这方面更容易使用。

这是我的代码的 sn-ps(正在进行中):

一个简单的类来保存 SelectListItem 的 IEnumerable:

public class Preference
{
    public string ControlType;          // "Select", "Textbox"
    public string Label;                // Label of control
    public IEnumerable<SelectListItem> ListItems;
}

MODEL(一个 IEnumerable 的 Preference 给了我“Preferences”)

public class UserPreferencesViewModel
{
    public IEnumerable<Preference> Preferences { get; set; }
}

控制器

List<Preference> Preferences = new List<Preference>();

foreach (var g in uomGroups)
{
    List<SelectListItem> listitems = new List<SelectListItem>();

    foreach (var w in g.Options)
    {
        ... Omitted for brevity ...

        listitems.Add( new SelectListItem { Text = w.UOMId, Value = w.UOMId, Selected = isSelected } )
    }

    var p = g.Options.ToArray();
    Preferences.Add( new Preference
    {
        ControlType = "Select",
        Label = g.Preference,
        ListItems = listitems
    });
}

model.Preferences = Preferences.AsEnumerable();

return View(model);

查看

<tbody>
@foreach (Preference preference in Model.Preferences)
{
    <tr>
        <td>
            @Html.LabelFor(
                x => x.Label, 
                new { @class = "col-md-3 control-label" }
                )
        </td>

        <td>
            @Html.DropDownListFor(
                x => x.Label,
                new SelectList(preference.ListItems, "Value", "Text"),
                new { @class = "col-md-3 form-control" }
                )
        </td>
    </tr>
}
</tbody>

从上面的代码中,有没有人看出问题出在哪里?我一直试图弄清楚这个几个小时,只是没有看到它。

【问题讨论】:

    标签: asp.net-mvc linq razor


    【解决方案1】:

    应该是

    @foreach (Preference preference in Model.Preferences)
    {
      ...
      @Html.LabelFor(i => preference.Label, ...
      ...
      @Html.DropDownListFor(i => preference.Label, preference.ListItems, ...
      ...
    

    编辑

    如果您需要唯一的 ID 和名称,那么它需要是一个 for 循环,以便它们被正确索引

    @for (int i = 0; i < Model.Preferences.Count; i++)
    {
      ...
      @Html.LabelFor(i => Model.Preferences[i].Label, ...
      ...
      @Html.DropDownListFor(i => Model.Preferences[i].Label, preference.ListItems, ..
      ...
    

    这将呈现像 html 一样

    <select id = "Preferences_1__Label" name="Preferences[1].Label" ...
    <select id = "Preferences_2__Label" name="Preferences[2].Label" ...
    

    【讨论】:

    • 我想我尝试了所有的可能性,但是那个。太感谢了。我现在看到另一个问题,我需要遍历ListItems,但你的回答肯定解决了我的问题。谢谢!
    • LabelFor 被呈现为 Label 并且 ID 被呈现为 preference_label 对于每个控件。你能明白为什么会发生这种情况吗?我已经跟踪了代码,.Label 肯定包含每个正确的字符串。
    • 如果您想要唯一的 ID 和名称(用于回发),您需要一个 for 循环。我会尽快更新答案
    • 我正在查看您的编码。我以前从来没有处理过这样的情况。我的意图是使用 .Label 的实际内容作为 ID,以便我可以在 POST 控制器操作中引用它的值并更新数据库中的用户配置文件。也许这是在单个对象中创建所有这些控件而不是在模型中单独引用它们的缺点?例如,.Label 可能包含“HeatLoad”,我实际上需要LabelFor 的文本和Html.DropdownListFor 的ID 就是这样。不行吗?
    • name 属性用于回发,而不是 id。 ID 仅在客户端用于选择元素。您始终可以使用Html.DropDownFor(....., new { id="someID", name="someName" } 设置它们,但如果您这样做,我会质疑您的方法。您尚未包含您的 post 方法,但如果它是 [HttpPost] public ActionResult(IEnumerable&lt;Preference&gt; model) {..,那么您需要根据我的编辑使用 for 循环,以便正确索引名称并且 ModelBinder 可以绑定您的模型。
    猜你喜欢
    • 2019-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 2020-10-29
    • 2011-03-31
    • 2012-06-03
    • 1970-01-01
    相关资源
    最近更新 更多