1

I have the following partial view which renders a drop down list:

 @model MartinDog.Core.Models.Section
 @{
    @Html.DropDownListFor(x=>x.Name
                          , new SelectList(Model.Dock.DockTemplate.Columns, 
                              "Id", "FriendlyName", 
                               Model.DockTemplateColumn.Id.ToString())
                          , new { @id = "ddlb_dockTemplateColumns" + 
                               Model.Id.ToString()})    
 }

I render it on my page like so:

@{Html.RenderPartial("_Admin_Page_DockTemplateColumnDropDown", Model);}

The partial view is rendered once for every Section object. A Section object one I've created and is editable in a jquery dialog box (change the name, display order, dock template column, etc.)

On the test page I am using, this Section dialog box is rendered four times (as there are four of them in my parent object).

The problem: *The SelectedValue in the SelectList for the drop down never gets set* - that is to say, the correct item in the drop down list is never selected when the dialog is displayed and I can't quite work out why.

I thought it might be because the drop down is rendered four times, so I tried rendering it for just one of the 'Sections' but still the same problem.

Anyone know what I can do?

***edit Not sure if I'm doing it in a sucky way. I had thought of building the dialog just once with jquery and json but I'd prefer to do it this way as it just feels cleaner.

adrianos
  • 1,501
  • 2
  • 18
  • 22

2 Answers2

0

I do this:

In controller action (Edit for example):

ViewData["ProvinceID"] = new SelectList(dc.Provinces.OrderBy(p => p.NameAr), "ID", "NameAr", factory.ProvinceID);

and Markup:

<%: Html.DropDownList("ProvinceID") %>

See? so it is a list of factories and it has a Province field and what I want you to notice is the 4th parameter in the SelectList constructor, I passed factory.ProvinceID so the DropDownList knows which option to be set on. Otherwise the DropDownList will show the default value (the first one).

P.S: It is your job to change to Razor syntax; I don't use it.

Hope that helps.

Ken D
  • 5,880
  • 2
  • 36
  • 58
  • I had a feeling someone might say do it in the controller (which I understand may well fixe the issue). Because my object model has everything I need for the drop down (full list and selected item), I don't need to populate it through the controller. I build the partial view(s) (including drop down) when I'm building the main view. Thanks though. – adrianos Jun 26 '11 at 11:37
0

Doh... fixed - totally my own fault.

I had set up my html.dropdownlistfor like so @Html.DropDownListFor(x=>x.Name,

When it should've been like so: @Html.DropDownListFor(x=>x.DockTemplateColumn.Id,

Setting the first argument to x=>x.DockTemplateColumn.Id (which uniquely identifies the items in my list) instead of x.Name fixed the issue straight away.

Just thought I'd post it here in case someone else makes the same mistake I did.

edit Found the answer here: C# mvc 3 using selectlist with selected value in view

Community
  • 1
  • 1
adrianos
  • 1,501
  • 2
  • 18
  • 22