DropdownlistFor 抛出错误
我使用下拉列表的硬编码字符串值到视图,并从数据库传递选定的值,其中 0 = 待处理,1 = 完成,3 = 等待,下面是视图和控制器的代码:
var paymentStatus = new[] { "Pending", "Complete", "AwaitingPayment" };
ViewData["StatusID"] = new SelectList(paymentStatus, "Value", "Text", booking.StatusID);
<tr><td>Status</td><td><%: Html.DropDownListFor(m => m.StatusID, ViewData["StatusID"] as SelectList)%></td></tr>
它出现错误: 数据绑定:“System.String”不包含名称为“Value”的属性。
I am using hardcoded string values for dropdownlist to the view , and passing the selected value from database , where 0 = pending , 1 = complete and 3 = awaiting, below is the code for view and controller:
var paymentStatus = new[] { "Pending", "Complete", "AwaitingPayment" };
ViewData["StatusID"] = new SelectList(paymentStatus, "Value", "Text", booking.StatusID);
<tr><td>Status</td><td><%: Html.DropDownListFor(m => m.StatusID, ViewData["StatusID"] as SelectList)%></td></tr>
It comes up with the error :
DataBinding: 'System.String' does not contain a property with the name 'Value'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的示例的问题在于您将字符串数组传递到
SelectList
中,然后告诉 SelectList 使用Value
和Text
属性(这是字符串所没有的)。您可能应该为此创建一个类:更好的是,为该数据创建一个存储库:
将其传递到您的 SelectList 中:
The problem with your example is that you are passing a string array into the
SelectList
and then telling the SelectList to use theValue
andText
properties (which a string does not have). You should probably create a class for this:Better yet, create a repository for this data:
Pass this into your SelectList:
请使用视图模型:
然后:
或者如果您坚持使用此
ViewData
(我不推荐它,特别是当您已经有了视图模型时):并在视图中:
Please use view models:
and then:
or if you insist on this
ViewData
(I don't recommend it, especially as you already have a view model):and in the view:
该错误表明它无法精细“Value”,您必须执行类似
bookin 的操作。Status 将是 booking 的任何文本属性。希望这有帮助
The error shows it is unable to Fine "Value" , you have to do something like
bookin.Status will be the any text property of booking. hope this help