Frage

Ich habe 2 Ansichten. ProductForm.aspx und Category.ascx. CategoryForm ist eine Teilansicht. Ich nenne die Category.ascx aus dem Product mit EditorFor (Modell => model.Category). In dieser Teilansicht gibt es eine DropdownlistFor mit alle Kategorie. Das Problem ist der gewählte Wert für eine bestimmte Produktkategorie. Die gewählte Wert dosen't Arbeit.

Warum?


Hier ist, was ich habe in meinem Product

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


in Category.ascx

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

Lösung

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 )%>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top