Asp.net mvc 剃须刀标签和滴落物

Asp.net mvc 剃须刀标签和滴落物,asp.net-mvc,linq,razor,Asp.net Mvc,Linq,Razor,我将MVC5与EF6一起使用。我需要在视图中创建几十个下拉列表,其中包含用户可选择的首选项,然后在整个web应用程序中使用 我遇到的问题是razor视图中的LabelFor和DropDownList for行都有错误: 编译器错误消息:CS0411:无法从用法推断方法“System.Web.Mvc.Html.LabelExtensions.LabelFor System.Web.Mvc.HtmlHelper,System.Linq.Expressions.Expression>,string)”

我将MVC5与EF6一起使用。我需要在视图中创建几十个下拉列表,其中包含用户可选择的首选项,然后在整个web应用程序中使用

我遇到的问题是razor视图中的LabelFor和DropDownList for行都有错误:

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

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

以下是我的代码片段(正在进行的工作):

保存SelectListItem的IEnumerable的简单类:

public class Preference
{
    public string ControlType;          // "Select", "Textbox"
    public string Label;                // Label of control
    public IEnumerable<SelectListItem> ListItems;
}
公共类首选项
{
公共字符串控件类型;/“选择”,“文本框”
公共字符串标签;//控件的标签
公共IEnumerable列表项;
}
模型(偏好的IEnumerable给我“偏好”)

public类UserPreferencesViewModel
{
公共IEnumerable首选项{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);
List Preferences=newlist();
foreach(uomGroups中的变量g)
{
List listitems=新列表();
foreach(g.期权中的var w)
{
…为简洁起见省略。。。
添加(新建SelectListItem{Text=w.UOMId,Value=w.UOMId,Selected=isSelected})
}
var p=g.Options.ToArray();
添加(新的首选项)
{
ControlType=“选择”,
标签=g.首选项,
ListItems=ListItems
});
}
model.Preferences=Preferences.AsEnumerable();
返回视图(模型);
查看

<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>

@foreach(Model.Preferences中的首选项)
{
@Html.LabelFor(
x=>x.标签,
新的{@class=“col-md-3控制标签”}
)
@Html.DropDownListFor(
x=>x.标签,
新建SelectList(preference.ListItems、“值”、“文本”),
新建{@class=“col-md-3表单控件”}
)
}
从上面的代码中,有人看到问题出在哪里了吗?我几个小时来一直想弄明白,但就是看不出来

应该是

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

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

@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, ..
  ...
@for(int i=0;iModel.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" ...

我想我已经尝试过了所有的可能性,除了那一个。非常感谢。我现在看到了另一个问题,我需要循环查看
列表项
,但是你的回答肯定解决了我问题中的问题。谢谢!标签
被呈现为
标签
,ID被呈现为
首选项标签
用于每个控件。您能看到为什么会发生这种情况吗?我已经跟踪了代码和
。标签
肯定包含每个控件的正确字符串。如果您想要唯一的ID和名称(用于回发)你需要为
循环设置一个
。我将简短地更新答案。我正在查看你编写的代码。我以前从未遇到过这样的情况。我的目的是使用
。Label
的实际内容作为ID,以便我可以在POST Controller操作中引用它的值,并更新数据库中的用户配置文件。也许在单个对象中创建所有这些控件而不是在模型中单独引用它们的缺点是什么?例如,
.Label
可能包含“热负荷”实际上,我需要
LabelFor
的文本和
Html.DropdownListFor
的ID才能做到这一点。不起作用吗?
name
属性用于回发,而不是
ID
。ID仅在客户端与选择元素相关。您可以始终使用
Html.DropDownFor(…,new{ID=“someID”)设置它们,name=“someName”}
,但如果您这样做,我会质疑您的方法。您没有包括post方法,但如果其
[HttpPost]公共操作结果(IEnumerable model){..
则需要根据我的编辑使用
for
循环,以便正确索引名称,ModelBinder可以绑定您的模型。
<select id = "Preferences_1__Label" name="Preferences[1].Label" ...
<select id = "Preferences_2__Label" name="Preferences[2].Label" ...