Asp.net mvc 4 MVC grid table dropdown selected value

Asp.net mvc 4 MVC grid table dropdown selected value,asp.net-mvc-4,razor,Asp.net Mvc 4,Razor,How to set selected value dropdown? The following do not set any selected value. @for (int i=0; i< Model.Students.Count;i++) { <tr> <td>@Html.TextBoxFor(x => Model.Students[i].Name, new { @class = "form-control" }) @Html.ValidationMessageFor(x => Model.Students[i].Name) </td> <td>@Html.TextBoxFor(x => Model.Students[i].Age, new

How to set selected value dropdown? The following do not set any selected value.

 @for (int i=0; i< Model.Students.Count;i++)
                {
                    <tr>
                        <td>@Html.TextBoxFor(x => Model.Students[i].Name, new { @class = "form-control" })
                        @Html.ValidationMessageFor(x => Model.Students[i].Name)
                         </td>
                        <td>@Html.TextBoxFor(x => Model.Students[i].Age, new { @class = "form-control" })</td>

                        <td>
                            @*@Html.DropDownList("country", Model.Countries,)*@

                            @Html.DropDownListFor(x => Model.Students[i].Country, Model.Countries)
                        </td>
                    </tr>
                }

ActionResult:

  public ActionResult ShowServerSideGrid()
    {
        var list = new List<SelectListItem>
       {
           new SelectListItem { Value = "1", Text = "USA" },
           new SelectListItem { Value = "2", Text = "UK" },
           new SelectListItem { Value = "3", Text = "China" },
           new SelectListItem { Value = "4", Text = "Russia" }
       };
        var students = new List<StudentModel>();
        students.Add(new StudentModel { Country = 1,Age = 24,Name = "Arjun"});
        students.Add(new StudentModel { Country = 2, Age = 24, Name = "Kumar" });
        students.Add(new StudentModel { Country = 3, Age = 45, Name = "Shiva" });
        students.Add(new StudentModel { Country = 4, Age = 60, Name = "Aryal" });
        StudentListModel listModel=new StudentListModel();
        listModel.Students = students;
        listModel.Countries = list;
        return View(listModel);
    }

#1

Please use following code to solve it

@Html.DropDownListFor(x => Model.Students[i].Country, new SelectList(Model.Countries,"Value","Text",Model.Students[i].Country))

#2

You need to use List<SelectListItem> dropDownValues {get;set;} in your model

Fill the dropDownValues feild with values in controller

Eg.

StudentListModel listModel=new StudentListModel();
listModel.dropDownValues .Add(new SelectListItem{Text = "USA", Value = "1"})

....... So on

and in your view you must use the syntax

@Html.DropDownFor(x => Model.Students[i].Country, Model.dropDownValues)

#3

look at the model being passed to the view. you are tying your drop down list for to Model.Students[i].Country. make sure that that field is set to a value that matches a value in Model.Countries

#4

If check the actionresult the value Country is set to match the values in selectlistitem

#5

it may have to do with the foreach. try putting a drop down list for outside of the foreach to see if it is set. Also try setting the selected property of one of the select list item to see if the drop downs change from that

#6

I tried outside loop but doesn't set: @Html.DropDownListFor(x => Model.Students[2].Country, Model.Countries)

#7

try setting the selected in your List<SelectListItem>

#8

selectedValue should be supplied when creating SelectList in the loop. So this should be marked as the correct answer.