代码之家  ›  专栏  ›  技术社区  ›  Jean-Francois

ASP.NET MVC 2、DropDownListfor和编辑器模板。选定值不起作用

  •  1
  • Jean-Francois  · 技术社区  · 14 年前

    我有两种观点。productForm.aspx和category.ascx。 CategoryForm是一个局部视图。我使用editorfor(model=>model.category)从productForm调用category.ascx。在这个部分视图中,有一个包含所有类别的DropDownList。问题是为特定产品类别选择的值。选定的值不起作用。

    为什么?


    这是我的产品表上的东西

    <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>
    
    1 回复  |  直到 14 年前
        1
  •  3
  •   asfsadf    14 年前

    可以将DDL的name属性分配给products表中调用的categoryID/foreign键。然后,您的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 )%>