5

I have a Html.DropDownListFor in an Editor Template which does dot set a selected value. If I replace the editor Template with a partial view(copy paste of HTML in editor template to the Partial View) it works.

sample code

Model:

public class MainItemViewModel
{
    public int MainItemId { get; set; }
    public ItemViewModel ItemViewModel { get; set; }
}

public class ItemViewModel
{
    public int ItemId { get; set; }
    public string Text { get; set; }
    public IEnumerable<SelectListItem> ItemSelectList { get; set; }
}

Controller:

public class DropDownController : Controller
{
    //
    // GET: /DropDown/

    public ActionResult Index()
    {
        var mainItemViewmodel = new MainItemViewModel();
        mainItemViewmodel.MainItemId = 2;
        mainItemViewmodel.ItemViewModel = new ItemViewModel();
        mainItemViewmodel.ItemViewModel.ItemId = 2;
        mainItemViewmodel.ItemViewModel.Text = "bla5";
        List<SelectListItem> someItems =  new List<SelectListItem>(){new SelectListItem(){Value = "1",Text = "Test1"},new SelectListItem(){Value = "2",Text = "Test2"}};
        mainItemViewmodel.ItemViewModel.ItemSelectList = someItems;
        return View(mainItemViewmodel);
    }

    [HttpPost]
    public ActionResult Index(MainItemViewModel mainItemViewModel)
    {
        return RedirectToAction("Index");
    }

}

DropDown\Index.chtml

@model MainItemViewModel


@using (Html.BeginForm())
{
 <br/>
 @Html.TextBoxFor(model => model.MainItemId)
 <br/>
 @Html.EditorFor(model => model.ItemViewModel)

 <input name="submit" type="submit" value="submit"/>
}

DropDown\EditorTemplates\ItemViewModel.chtml

   @model ItemViewModel
   @{
   @Html.DropDownListFor(model => model.ItemId, Model.ItemSelectList, "Select Item");
   <br/>

    @Html.TextBoxFor(model => model.Text);
   }

The DropDownListFor(model => model.ItemId, Model.ItemSelectList, "Select Item"); does not select 2.I thought it is actually supposed to automatically select for you

I have been doing the same thing except having another view model within another for all my dropdownlist and it has worked fine.

Is there a problem with editor Templates and dropdownlist,I have looked at most answers to this problem and I do not see any conclusive answer that works I have looked at these solutions

Problem binding selected value to DropDownListFor inside Editor Template

DropDownListFor - does not select “Selected” value

Community
  • 1
  • 1
kwiri
  • 1,399
  • 2
  • 15
  • 22

1 Answers1

0

I suppose you've a property named ItemId in your ItemViewModel

Then just be sure to set it's value in the Controller before sending the ViewModel to the View:

public ActionResult Edit()
{
   var viewModel = new MainViewModel
                              {
                                  ItemViewModel = new ItemViewModel{ ItemId = 7 }
                              };
   return View(viewModel);
}

If you want more options take a look at my answer here

Community
  • 1
  • 1
Wahid Bitar
  • 13,776
  • 13
  • 78
  • 106
  • I am doing exactly that by setting the ItemId before sending the ViewModel.If I use a partial view with exact same model it works.My question would be why does it work with a Partial view(same model) instead of the editorFor.Right now I am using the partial View – kwiri Jun 19 '12 at 05:10
  • Yes I have thanks but it is not what I was really looking for.I have done some debugging with the actual MVC source(RTM3) and I have seen that they maybe something not right.It breaks beacause of the way the property name is used to get the default selected value.This is why my dropdownlistFor in the editorFor Template does not select the correct value while in a Partial view it does work.I made a simple test app using the mvc source and it has proved the dropdown breaks.Strange I did notice that for textboxes a different method of getting the property values was used. – kwiri Jun 19 '12 at 14:19
  • The idea is I would like to use the dropdownlistfor the normal way without having to add extra code to make a selection.I have been using the dropdownlist like this and it has worked fine, it is only this particular view where I need to throw in the editor template (withe the dropdown) in a view like what I have described above – kwiri Jun 19 '12 at 14:27
  • I have edited the question to show the mini example.All I am asking is it not normal for it to just automatically select as in normal views,I just want to avoid extra code just make to make it work for these cases, and it might be for a lot.It does not work for me to use a partial view as it just does not work well for editing..you cannot submit the whole view and expect to get values – kwiri Jun 19 '12 at 15:11
  • @kwiri +1 You're absolutely right it's kinda bug. then you should set the selected value to true in the controller `someItems = new List() {new SelectListItem() {Value = "1", Text = "Test1"}, new SelectListItem() {Value = "2", Text = "Test2",Selected = true}};` – Wahid Bitar Jun 19 '12 at 15:28
  • 1
    That is what I was thinking too ,debugging with source(ASP.NET MVC source) shows that the code for dropdownlistfor actauly fails to get the value for the property ItemViewModel.ItemId. I wonder if this has this been submitted as a bug or it is the way it works, which breaks the whole idea that it should automatically select – kwiri Jun 19 '12 at 16:47