Html.DropDownListFor 不绑定 boolean SelectList
我有这段代码构造一个选择列表作为布尔响应
var responseList = new List<SelectListItem>();
responseList.Add(new SelectListItem { Text = "Going", Value = bool.TrueString});
responseList.Add(new SelectListItem { Text = "Not Going", Value = bool.FalseString });
ViewData[ViewDataKeys.ResponseTo] = vatOptionList;
在我看来,我使用下面的下拉列表助手。
@Html.DropDownListFor(m => m.ResponseTo, (IEnumerable<SelectListItem>)ViewData[ViewDataKeys.ResponseTo], "--Select--")
这是我的 Model 类上的属性:
[Display(Name = "Response To")]
public bool ResponseTo { get; set; }
我的问题是,无论 model.ResponseTo 的值是什么,下拉列表总是选择可选值。
我尝试使用复选框助手,令人惊讶的是它似乎也没有被选中,尽管当我检查元素时,复选框值为“true”,
我尝试使用文本框助手,它显示了“true”文本,其中我认为我的模型有价值,它只是不绑定到下拉列表或复选框。我需要使用下拉列表。我错过了什么吗?
I have this code the constructs a select list as a boolean response
var responseList = new List<SelectListItem>();
responseList.Add(new SelectListItem { Text = "Going", Value = bool.TrueString});
responseList.Add(new SelectListItem { Text = "Not Going", Value = bool.FalseString });
ViewData[ViewDataKeys.ResponseTo] = vatOptionList;
In my view I use the dropdownlist helper below.
@Html.DropDownListFor(m => m.ResponseTo, (IEnumerable<SelectListItem>)ViewData[ViewDataKeys.ResponseTo], "--Select--")
this is the property on my Model class:
[Display(Name = "Response To")]
public bool ResponseTo { get; set; }
My problem is that what ever the value of my model.ResponseTo is, the dropdownlist always select the optional value.
I tried to use a checkbox helper and surprisingly it doesn't appear to be checked alse, though when I inspected the element, the checkbox value is "true"
I tried to use a textbox helper and it shows a "true" text, Which I think that my model has value and it just doesn't bind to the dropdownlist or checkbox. I need to use a dropdownlist. Anything I missed out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
刚刚对此进行了测试,它有效:
在您的操作方法中:
在您看来:
Just tested this, it works:
In your action method:
In your view: