DropdownlistFor throws an error

2019-08-29 01:07发布

问题:

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'.

回答1:

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);


回答2:

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>


回答3:

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