1

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);
    }
6
  • 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 Commented Feb 4, 2014 at 2:42
  • If check the actionresult the value Country is set to match the values in selectlistitem Commented Feb 4, 2014 at 2:45
  • 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 Commented Feb 4, 2014 at 2:52
  • I tried outside loop but doesn't set: @Html.DropDownListFor(x => Model.Students[2].Country, Model.Countries) Commented Feb 4, 2014 at 2:59
  • try setting the selected in your List<SelectListItem> Commented Feb 4, 2014 at 3:07

2 Answers 2

2

Please use following code to solve it

@Html.DropDownListFor(x => Model.Students[i].Country, new SelectList(Model.Countries,"Value","Text",Model.Students[i].Country))
Sign up to request clarification or add additional context in comments.

1 Comment

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

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.