DropdownlistFor 抛出错误

发布于 2024-11-23 18:26:37 字数 482 浏览 3 评论 0原文

我使用下拉列表的硬编码字符串值到视图,并从数据库传递选定的值,其中 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 技术交流群。

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

发布评论

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

评论(3

青衫负雪 2024-11-30 18:26:37

您的示例的问题在于您将字符串数组传递到 SelectList 中,然后告诉 SelectList 使用 ValueText 属性(这是字符串所没有的)。您可能应该为此创建一个类:

public class Status {
    public int Id { get; set; }
    public string Text { get; set; }
}

var statusTypes = new List<Status> {
    new Status { Id = 1, Text = "Pending" },
    new Status { Id = 2, Text = "Complete" },
    new Status { Id = 3, Text = "AwaitingPayment" }
};

更好的是,为该数据创建一个存储库:

var statusTypes = statusRepository.GetStatusTypes();

将其传递到您的 SelectList 中:

SelectList statusList = new SelectList(statusTypes, "Id", "Text", booking.StatusID);

// return this in a ViewModel or use ViewData like you are now:
ViewData["Status"] = statusList;

return View(statusList);

The problem with your example is that you are passing a string array into the SelectList and then telling the SelectList to use the Value and Text properties (which a string does not have). You should probably create a class for this:

public class Status {
    public int Id { get; set; }
    public string Text { get; set; }
}

var statusTypes = new List<Status> {
    new Status { Id = 1, Text = "Pending" },
    new Status { Id = 2, Text = "Complete" },
    new Status { Id = 3, Text = "AwaitingPayment" }
};

Better yet, create a repository for this data:

var statusTypes = statusRepository.GetStatusTypes();

Pass this into your SelectList:

SelectList statusList = new SelectList(statusTypes, "Id", "Text", booking.StatusID);

// return this in a ViewModel or use ViewData like you are now:
ViewData["Status"] = statusList;

return View(statusList);
青瓷清茶倾城歌 2024-11-30 18:26:37

请使用视图模型:

var paymentStatuses = new[]
{
    new SelectListItem { Value = "0", Text = "Pending" },
    new SelectListItem { Value = "1", Text = "Complete" },
    new SelectListItem { Value = "3", Text = "AwaitingPayment" },
};
var model = new SomeViewModel
{
    StatusID = booking.StatusID,
    Statuses = new SelectList(paymentStatuses, "Value", "Text")
}
return View(model);

然后:

<tr>
    <td>
        Status
    </td>
    <td>
        <%= Html.DropDownListFor(m => m.StatusID, Model.Statuses) %>
    </td>
</tr>

或者如果您坚持使用此 ViewData (我不推荐它,特别是当您已经有了视图模型时):

var paymentStatuses = new[]
{
    new SelectListItem { Value = "0", Text = "Pending" },
    new SelectListItem { Value = "1", Text = "Complete" },
    new SelectListItem { Value = "3", Text = "AwaitingPayment" },
};
ViewData["Statuses"] = new SelectList(paymentStatuses, "Value", "Text");
var model = new SomeViewModel
{
    StatusID = booking.StatusID
}
return View(model);

并在视图中:

<tr>
    <td>
        Status
    </td>
    <td>
        <%= Html.DropDownListFor(m => m.StatusID, ViewData["Statuses"] as SelectList) %>
    </td>
</tr>

Please use view models:

var paymentStatuses = new[]
{
    new SelectListItem { Value = "0", Text = "Pending" },
    new SelectListItem { Value = "1", Text = "Complete" },
    new SelectListItem { Value = "3", Text = "AwaitingPayment" },
};
var model = new SomeViewModel
{
    StatusID = booking.StatusID,
    Statuses = new SelectList(paymentStatuses, "Value", "Text")
}
return View(model);

and then:

<tr>
    <td>
        Status
    </td>
    <td>
        <%= Html.DropDownListFor(m => m.StatusID, Model.Statuses) %>
    </td>
</tr>

or if you insist on this ViewData (I don't recommend it, especially as you already have a view model):

var paymentStatuses = new[]
{
    new SelectListItem { Value = "0", Text = "Pending" },
    new SelectListItem { Value = "1", Text = "Complete" },
    new SelectListItem { Value = "3", Text = "AwaitingPayment" },
};
ViewData["Statuses"] = new SelectList(paymentStatuses, "Value", "Text");
var model = new SomeViewModel
{
    StatusID = booking.StatusID
}
return View(model);

and in the view:

<tr>
    <td>
        Status
    </td>
    <td>
        <%= Html.DropDownListFor(m => m.StatusID, ViewData["Statuses"] as SelectList) %>
    </td>
</tr>
甜警司 2024-11-30 18:26:37

该错误表明它无法精细“Value”,您必须执行类似

new SelectList(paymentStatus, booking.Status, "Text", booking.StatusID)

bookin 的操作。Status 将是 booking 的任何文本属性。希望这有帮助

The error shows it is unable to Fine "Value" , you have to do something like

new SelectList(paymentStatus, booking.Status, "Text", booking.StatusID)

bookin.Status will be the any text property of booking. hope this help

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