MVC C# DropDownListFor 具有前一个 select 的值
MVC C# DropDownListFor has value of previous select
C# fiddle screenshot
https://dotnetfiddle.net/MadhI5
型号:
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 (正确)
查看 fiddle 演示。为什么 Val2 和 Val4 在 View 中不为空?这是一个框架错误?
...
vm.Val1 = "3";
vm.Val2 = "";
vm.Val3 = "1";
vm.Val4 = "";
vm.Val5 = "3";
...
使用 "" 代替 null 有效
C# fiddle screenshot
https://dotnetfiddle.net/MadhI5
型号:
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 (正确)
查看 fiddle 演示。为什么 Val2 和 Val4 在 View 中不为空?这是一个框架错误?
...
vm.Val1 = "3";
vm.Val2 = "";
vm.Val3 = "1";
vm.Val4 = "";
vm.Val5 = "3";
...
使用 "" 代替 null 有效