ASP.NET MVC 2,下拉列表和编辑模板。选定的值不起作用
题
我有2个意见。 productform.aspx and cattory.ascx。类别形式是部分视图。我使用Editorfor(model => model.Category)从productform中调用category.ascx。在此部分视图中,有一个与所有类别的下拉列表。问题是特定产品类别的选定值。所选值不起作用。
为什么 ?
这是我的产品形式中的
<div class="editor">
<div class="editor-label">
<%: Html.LabelFor(model => model.ProductInfo.ProductName) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.ProductInfo.ProductName)%>
<%: Html.ValidationMessageFor(model => model.ProductInfo.ProductName)%>
</div>
</div>
<%: Html.EditorFor(model => model.ProductInfo.Category, new { CategoryList = Model.CategoryList })%>
在类别中
<div class="editor-field">
<%:Html.DropDownListFor(model => model.CategoryID, (IEnumerable<SelectListItem>)ViewData["CategoryList"])%>
</div>
解决方案
您可以将DDL的名称属性分配给产品表中的任何类别/外键。然后,由于默认绑定的工作方式,您的DDL将自动选择该类别。
一个例子:
<%: Html.DropDownList("Book.GenreID" , Model.GenresSelectList )%>
以及由此产生的HTML:
<select id="Book_GenreID" name="Book.GenreID">
<option value="2">Horror</option>
<option selected="selected" value="3">Literature</option>
<option value="1">Science Fiction</option>
</select>
或者:
<%: Html.DropDownListFor(model => model.Book.GenreID, Model.GenresSelectList )%>
不隶属于 StackOverflow