0

I have multiple dropdownlists on a page which are being built at runtime. I have a view model with a selectlist. I have tried this post which is great but it is a bit different coz i have a collection of view model on the view. I think I am close but i am not sure how to use DropDownListFor properly. How can I preselect each dropdownlist with different values?

VM

public class categoryVm
{        
    public IEnumerable<SelectListItem> category { get; set; }

    public string SelectedValue { get; set; }
} 

View

@model IEnumerable<categoryVm>
@foreach (var item in Model)
{
   //i think i am missing something here???
   @Html.DropDownListFor(**x => item.SelectedValue**, item.category)
}

Controller

name is one of StaticName and it's like an Id and it's in the list.

var categories = new List<categoryVm>();
var selectlistcolumns = columns.Select(x => new SelectListItem
            {                    
                Value = x.StaticName,
                Text = x.DisplayName
            });

foreach (var column_loopVariable in dtCSV.Columns)
            {
                var category= new categoryVm();                    
                category.category= selectlistcolumns;
                category.SelectedValue = "name";
                categories .Add(category);
            }
     return View(categories);

I would prefer mvc solution than using javascript changing dom.

Community
  • 1
  • 1
Laurence
  • 7,633
  • 21
  • 78
  • 129

3 Answers3

3

Try this

var selectlistcolumns = columns.Select(x => new SelectListItem
        {                    
            Value = x.StaticName,
            Text = x.DisplayName,
            Selected = x.DisplayName == "name"  // this sets the selected value
        });

foreach (var column_loopVariable in dtCSV.Columns)
        {
            var category= new categoryVm();                    
            category.category= selectlistcolumns;
            //category.SelectedValue = "name"; // not required
            categories .Add(category);
        }

Also if you notice the signature for Html.DropDownListFor(propExpresstion, selectList) so the first argument as you assumed does not set the selected value. Hope this helps!

cackharot
  • 688
  • 1
  • 6
  • 13
1

Since your controller return List. You should use IList in view page instead of IEnumerable.

@model IList<categoryVm>

for (int i = 0; i < Model.Count; i++) 
{
   @Html.DropDownListFor(m => Model[i].SelectedValue, Model[i].category)
}

View Model IEnumerable<> property is coming back null (not binding) from post method?

Community
  • 1
  • 1
Ashwini Verma
  • 7,477
  • 6
  • 36
  • 56
  • 1
    Either this method, or another option is to make an EditorTemplate for "categoryvm", in which case you can simply call Html.EditorFor and MVC will automatically apply proper indexing to your fields – Moeri Apr 02 '14 at 10:01
0
@Html.DropDownListFor(m => item.SelectedValue, new SelectList(item.category))
DHN
  • 4,807
  • 3
  • 31
  • 45
Murali Murugesan
  • 22,423
  • 17
  • 73
  • 120