Html.DropDownListFor 不绑定 boolean SelectList

发布于 2024-12-09 20:38:48 字数 818 浏览 2 评论 0原文

我有这段代码构造一个选择列表作为布尔响应

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木槿暧夏七纪年 2024-12-16 20:38:48

刚刚对此进行了测试,它有效:

在您的操作方法中:

var selectListItems = new List<SelectListItem>();
selectListItems.Add(new SelectListItem { Text = "Going", Value = bool.TrueString });
selectListItems.Add(new SelectListItem { Text = "Not going", Value = bool.FalseString });
ViewBag.MySelectList = new SelectList(selectListItems, "Value", "Text", viewModel.IsGoing);

在您看来:

@Html.DropDownList("IsGoing", (SelectList) ViewBag.MySelectList)

Just tested this, it works:

In your action method:

var selectListItems = new List<SelectListItem>();
selectListItems.Add(new SelectListItem { Text = "Going", Value = bool.TrueString });
selectListItems.Add(new SelectListItem { Text = "Not going", Value = bool.FalseString });
ViewBag.MySelectList = new SelectList(selectListItems, "Value", "Text", viewModel.IsGoing);

In your view:

@Html.DropDownList("IsGoing", (SelectList) ViewBag.MySelectList)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文