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