https://www.amazon.com/gp/help/customer/display.html?nodeId=14113001
型号:
public class IndexViewModel
{
public List<SelectListItem> Sel { get; set; }
public string Val1 { get; set; }
public string Val2 { get; set; }
public string Val3 { get; set; }
public string Val4 { get; set; }
public string Val5 { get; set; }
}
控制器:
public ActionResult Index()
{
var vm = new IndexViewModel();
vm.Sel = new List<SelectListItem>();
vm.Sel.Add(new SelectListItem() { Text = "Opt1", Value = "1" });
vm.Sel.Add(new SelectListItem() { Text = "Opt2", Value = "2" });
vm.Sel.Add(new SelectListItem() { Text = "Opt3", Value = "3" });
vm.Val1 = "3";
vm.Val2 = null;
vm.Val3 = "1";
vm.Val4 = null;
vm.Val5 = "3";
return View(vm);
}
查看:
@model HelloWorldMvcApp.IndexViewModel
@{
Layout = null;
}
@Html.DropDownListFor(m => m.Val1, Model.Sel, "-")
@Html.DropDownListFor(m => m.Val2, Model.Sel, "-")
@Html.DropDownListFor(m => m.Val3, Model.Sel, "-")
@Html.DropDownListFor(m => m.Val4, Model.Sel, "-")
@Html.DropDownListFor(m => m.Val5, Model.Sel, "-")
所选值为
Val1
:3(正确)Val2
:3(应为“ - ”)Val3
:1(正确)Val4
:1(应为“ - ”)Val5
:3(正确)看看小提琴演示。为什么Val2和Val4在View中不为空?这是一个框架错误?
答案 0 :(得分:1)
...
vm.Val1 = "3";
vm.Val2 = "";
vm.Val3 = "1";
vm.Val4 = "";
vm.Val5 = "3";
...
使用“”代替null工作