我正在尝试在局部视图中设置列表的默认值。使用this ...
调用局部视图 @Html.Action("Acton", "Controller", new { department = item.Data.departmentNumber, defaultValue="someValue" })
然后在控制器中我......
[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);
}
为什么我选择的值不会被添加到我的SelectListItem中,因为“Text”和“Value”属性是?谢谢你的帮助!
答案 0 :(得分:1)
假设代码中的值是您正在使用的文字值,“defaultValue”是“someValue”,您使用比较defalutValue ==“defaultValueGetsSentToView”进行选择。
“someValue”==“defaultValueGetsSentToView”的计算结果为false。