Reputation: 46322
I have a list which I created in the controller:
var PayList = new[] {
new ListEntry { Id = 1, Name = "" },
new ListEntry { Id = 2, Name = "Yes" },
new ListEntry { Id = 3, Name = "No" }
};
ViewBag.PayList = new SelectList(PayList, "Id", "Name");
In the view I have the following:
@Html.DropDownList("Pay", new SelectList(ViewBag.PayList,"Id","Name"))
When I try to display it, it says the following: DataBinding: 'System.Web.Mvc.SelectListItem' does not contain a property with the name 'Id'. Not sure why this is not working.
Also how do I default a value to the select list. I like to default it to "Yes". I thought there was a way to do do this from the controller.
Upvotes: 4
Views: 22642
Reputation: 54656
Your ViewBag.PayList
is already the type of SelectList
. I don't see a reason to create the SelectList
twice, so shouldn't it just be:
@Html.DropDownList("Pay", ViewBag.PayList)
or
@Html.DropDownList("Pay", ViewBag.PayList as SelectList)
(I don't ever use the ViewBag, so I'm not sure if your version is strongly typed).
Upvotes: 8
Reputation: 11
Try this Way:
<div id="divmsg" style="color: green; font-weight: bold">
@ViewBag.Msg
</div>
<div id="divmsg2" style="color: red; font-weight: bold">@ViewBag.Msg2</div>
<div id="quality" style="width: 80%;" align="center">
<input type="hidden" value="@ViewBag.ProjectId" id="ProjectId_" class="projectId"/>
<input type="hidden" value="@ViewBag.ProjectName" id="ProjectName_" class="projectName"/>
<input type="hidden" value="@ViewBag.UserId" class="UserId_" id="UserId"/>
<input type="hidden" value="@ViewBag.TempId" class="TempId_" id="TempId" />
<div class="toggle-contents">
<table width="100%" id="qualitygoal">
<tr>
<td class="even" align="left">
@Html.Label("Project Id")
</td>
<td class="even" align="left">
@ViewBag.ProjectId
</td>
</tr>
<tr>
<td class="projectname" align="left">
@Html.Label("Project Name")
</td>
<td class="projectname" align="left">
@ViewBag.ProjectName
</td>
</tr>
</table>
<table width="100%" id="qualitygoal1" class="tbl">
<tbody>
<tr>
<th align="center">DestinationColumns</th>
<th align="center">SourceColumns</th>
</tr>
@foreach (var data in Model)
{
<tr>
<td>
<span class="spanStatus" id="[email protected]" destinationID = "@data.Destination">@data.Destinationvalue</span>
<select class="status" id="[email protected]">
<option value="0">--Select--</option>
<option value="4">TICKET ID</option>
<option value="5">DESCRIPTION</option>
<option value="6">TICKET CATEGERY</option>
<option value="7">SEVIORITY/PRIORITY</option>
<option value="8">STATUS</option>
<option value="9">CREATED DATE</option>
<option value="10">CREATED BY</option>
<option value="11">ASSIGNED TO</option>
<option value="12">ASSIGNED DATE</option>
<option value="13">REPSONSE ETA</option>
<option value="14">RESOLUTION ETA</option>
<option value="15">RESPONSE DATE</option>
<option value="16">RESOLUTION DATE</option>
<option value="17">ROOT CAUSE/MODULE</option>
<option value="18">REOPEN FLAG (Y/N)</option>
<option value="19">CLOSE DATE</option>
<option value="20">SLA MET (Y/N)</option>
</select>
</td>
<td>
<span class="spanSource" id="[email protected]" >@data.Source</span>
<input class="Source" id="[email protected]" type="text" value="@data.Source" maxlength="30" />
</td>
<td>
<table style="width: 50%;">
<tr>
<td>
<input class="edit" id="[email protected]" type="button" value="Edit" />
<input class="update" id="[email protected]" type="button" value="Update" />
</td>
<td class="Gcancle" id="[email protected]">
<input class="gridcancel" id="[email protected]" type="button" value="Cancel" />
</td>
</tr>
</table>
</td>
<td>
<table style="width: 50%;">
<tr>
<td>
<input class="delete" id="[email protected]" type="button" value="Delete" />
</td>
</tr>
</table>
</td>
</tr>
}
</tbody>
</table>
</div>
<div align="right">
<input type="button" value="Add New Row" class="Add" />
<input type="button" value="Save" class="saved" />
<input type="button" value="Close" class="cancel" />
</div>
</div>
Upvotes: 0
Reputation: 41767
Just use
@Html.DropDownList("Pay", ViewBag.PayList)
In your view
Upvotes: 6