1

我有2意見。 ProductForm.aspx和Category.ascx。 CategoryForm是部分視圖。我使用EditorFor從ProductForm調用Category.ascx(model => model.Category)。在這個部分視圖中,有一個包含所有類別的DropdownlistFor。問題是特定產品類別的選定值。選定的值不起作用。Asp.Net mvc 2,DropDownListFor和編輯器模板。選定的值不d123

爲什麼?


以下是我在我的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> 
+0

這些是兩個單獨的形式?結果HTML是什麼樣的? – asfsadf 2010-09-24 18:27:57

+0

是2個獨立的文件。 ProductForm.apsx是主視圖。 Category.ascx是由ProductForm調用的部分視圖在Category.ascx中,存在具有類別List的下拉列表。 – 2010-09-24 18:45:32

+0

我沒有看到任何明顯的需要將這些分開。我將擁有一個包含Product對象的強類型視圖模型,以及CategoriesSelectList,它將以相同的形式出現。 – asfsadf 2010-09-24 18:56:11

回答

3

您可以將DDL的name屬性分配給您的任何類別ID /您的產品表中調用外鍵。然後,由於缺省綁定的工作方式,您的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)%> 
+0

我已經有一個類別和產品之間的外鍵如果我把類別下拉列表放在我的ProductForm中,一切正常,但我的問題是,我打電話給CategoryForm productForm with EditorFor。 – 2010-09-24 18:08:14

+0

你知道爲什麼DropDownListFor工作嗎??? – 2010-09-24 18:13:57

+0

它適用於上面的例子。粘貼你的代碼,也許,如果它不工作。 – asfsadf 2010-09-24 18:15:50