0

I am trying to set a default value of a list in a partial view. The partial view is called using this…

    @Html.Action("Acton", "Controller", new { department = item.Data.departmentNumber, defaultValue="someValue" })

Then in the controller I have…

  [ChildActionOnly]
    public ActionResult Categories(int? id, int? department, string defaultValue)
    {
        var typeList = from e in db.Rubrics where e.DepartmentID == department select e;
        var selectedRubrics = typeList.Select(r => r.Category);

        List<String> rubricsList = selectedRubrics.ToList();

                   var categories = new List<SelectListItem>();             
        for (int i = 0; i < rubricsList.Count(); i++)
        {
            categories.Add(new SelectListItem
            {
                Text = rubricsList[i],
                Value = rubricsList[i],
                Selected = (defaultValue == "defaultValueGetsSentToView")
            });
        }


        var ViewModel = new RubricsViewModel
        {
            Category = "Select a Category",
            Categories = categories


        };

        return View(ViewModel);





    }

Why does my selected value not get added to my SelectListItem as the “Text” and “Value” properties are? Thanks for any help!

1 Answer 1

1

Assuming the values in the code are the literal values you are using, "defaultValue" is "someValue" and you are setting selected with the comparison defalutValue == "defaultValueGetsSentToView".

"someValue" == "defaultValueGetsSentToView" evaluates to false.

4
  • How can I set the selected default value with some string? Jun 3, 2014 at 17:43
  • I'm not going to directly answer it just yet. I'll point you in the right direction though. Keep in mind my initial response.
    – tdbeckett
    Jun 4, 2014 at 19:03
  • you are going to have to change this line of code @Html.Action("Acton", "Controller", new { department = item.Data.departmentNumber, defaultValue="someValue" })
    – tdbeckett
    Jun 4, 2014 at 19:03
  • you are going to have to change this line of code too Selected = (defaultValue == "defaultValueGetsSentToView")
    – tdbeckett
    Jun 4, 2014 at 19:04

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.