1

我有 2 个视图。ProductForm.aspx 和 Category.ascx。CategoryForm 是一个局部视图。我使用 EditorFor(model => model.Category) 从 ProductForm 调用 Category.ascx。在这个局部视图中,有一个包含所有类别的 DropdownlistFor。问题是特定产品类别的选定值。所选值不起作用。

为什么 ?


这是我的 ProductForm 中的内容

<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 })%>


在 Category.ascx

<div class="editor-field">
   <%:Html.DropDownListFor(model => model.CategoryID, (IEnumerable<SelectListItem>)ViewData["CategoryList"])%>
</div>
4

1 回答 1

3

You can assign the name attribute of your DDL to whatever your CategoryID/foreign key is called in your Products table. Then your DDL will automatically select that category, due to how default binding works.

One example:

<%: Html.DropDownList("Book.GenreID" , Model.GenresSelectList )%>

and the resulting 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>

or:

<%: Html.DropDownListFor(model => model.Book.GenreID, Model.GenresSelectList )%>
于 2010-09-24T18:01:24.487 回答