1

I Have 2 views. ProductForm.aspx and Category.ascx. CategoryForm is a Partial View. I Call the Category.ascx from the ProductForm with EditorFor(model => model.Category) . In this partial view, there is a DropdownlistFor with all the category. The problem is the Selected Value for a specific Product Category. The selected value dosen't work.

Why ?


Here is what I have in my 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 })%>


In Category.ascx

<div class="editor-field">
   <%:Html.DropDownListFor(model => model.CategoryID, (IEnumerable<SelectListItem>)ViewData["CategoryList"])%>
</div>
Jean-Francois
  • 1,899
  • 4
  • 35
  • 73
  • These are two separate forms? What does the resulting HTML look like? – asfsadf Sep 24 '10 at 18:27
  • Yes 2 separate files. ProductForm.apsx is the main view. Category.ascx is the Partial View called by the ProductForm In the Category.ascx, there is the dropdownlist with the category List. – Jean-Francois Sep 24 '10 at 18:45
  • I don't see any obvious need to have these separated. I would have a strongly typed view model that included a Product object, along with the CategoriesSelectList, which will go in the same form. – asfsadf Sep 24 '10 at 18:56
  • you absolutely right . In this context, I should just put this dropdownlist in the same view. Here is just a basic example to understand my problem and try to resolved it. I would like to use Editor For in many other context because that's completely DRY. – Jean-Francois Sep 24 '10 at 18:59
  • Perhaps this question offers similar explanations: http://stackoverflow.com/questions/1235646/asp-net-mvc-2-html-editorfor-and-custom-editortemplates I'm having some difficulty ascertaining precisely what you're asking. – asfsadf Sep 24 '10 at 19:18
  • 1
    @olishedTurd : thanks for your help. The problem was only me for this issue. Here is what I was doing wrong. In the controller, I created an IEnumerable. The problem here is the Key and the Value. The key is not CategoryID, it was "Value". Here is the reason why my dropdown didn't select the proper value. Here is the final code <%:Html.DropDownListFor(model => model.CategoryID, new SelectList((IEnumerable)ViewData["CategoryList"],"Value","Text",Model != null ? Model.CategoryID : -2) – Jean-Francois Sep 24 '10 at 20:54

1 Answers1

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 )%>
asfsadf
  • 3,822
  • 7
  • 31
  • 41
  • I Already have a foreign key between category and Product. IF I put the Category dropdownlist in my ProductForm, all works well. But my problem is that I call the CategoryForm in the productForm with EditorFor. – Jean-Francois Sep 24 '10 at 18:08
  • It works for the above example. Paste your code, maybe, if it's not working. – asfsadf Sep 24 '10 at 18:15