Razor 中的下拉问题
我在数据库中有 2 个表:MixedType(id 和 name)和 Block(id,name,idMixedType)。
我想为块(创建视图)创建强类型视图。
控制器如下:
public ActionResult Create()
{
return View();
}
Block()是一个分部类(我使用Entity Framework + POCO)。
我对文本字段没有问题,它工作正常:
<div class="editor-label">
@Html.LabelFor(model => model.name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.name)
@Html.ValidationMessageFor(model => model.name)
</div>
但我想使用 MixedType 表中的值为 idMixedType 字段创建下拉列表。
我尝试按照以下方式执行此操作(根据此答案 使用实体框架(.edmx 模型)和 Razor 视图创建 MVC3 的下拉列表 && 将数据库记录插入到多个表):
<div class="editor-label">
@Html.LabelFor(model => model.idMixedType)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.idMixedType, new SelectList(Model.MixedType, "id", "name"))
@Html.ValidationMessageFor(model => model.idMixedType)
</div>
但我有一个错误出了
The best overloaded method match for 'System.Web.Mvc.SelectList.SelectList(System.Collections.IEnumerable, string, string)' has some invalid arguments
什么问题?
I have 2 table in db: MixedType(id and name) and Block(id, name, idMixedType).
I want to make strongly-typed view for Block (Create view).
Controller is following:
public ActionResult Create()
{
return View();
}
Block() is a partial class (I use Entity Framework + POCO).
I have no problem with text fields, it works fine:
<div class="editor-label">
@Html.LabelFor(model => model.name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.name)
@Html.ValidationMessageFor(model => model.name)
</div>
But I want to make dropdown for idMixedType field with values from MixedType table.
I tried to do it in following way (according to this answer Create a Dropdown List for MVC3 using Entity Framework (.edmx Model) & Razor Views && Insert A Database Record to Multiple Tables):
<div class="editor-label">
@Html.LabelFor(model => model.idMixedType)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.idMixedType, new SelectList(Model.MixedType, "id", "name"))
@Html.ValidationMessageFor(model => model.idMixedType)
</div>
But I have a error
The best overloaded method match for 'System.Web.Mvc.SelectList.SelectList(System.Collections.IEnumerable, string, string)' has some invalid arguments
What is wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将
Model.MixedType
传递给SelectList
构造函数。据推测Model.MixedType
不是 IEnumerable。是否有可能应该是小写“m”(model.MixedType)?
如果没有,您需要检查静态
MixedType
属性,并确保它是一个实现IEnumerable
的集合(并且它枚举的对象具有“id”和“name”属性,但我认为情况就是如此)。You're passing in
Model.MixedType
to theSelectList
constructor. PresumablyModel.MixedType
is not IEnumerable.Is it possible it should be a lowercase "m" (model.MixedType)?
If not, you need to review the static
MixedType
property and make sure it is a collection that implementsIEnumerable
(and that the objects it enumerates have "id" and "name" properties, but I presume that's the case).