我有一个我在控制器中创建的列表:
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");
在视图中我有以下内容:
@Html.DropDownList("Pay", new SelectList(ViewBag.PayList,"Id","Name"))
当我尝试显示它时,它会显示以下内容: DataBinding:'System.Web.Mvc.SelectListItem'不包含名为'Id'的属性。不知道为什么这不起作用。
另外,如何将值默认为选择列表。我喜欢将其默认为“是”。我认为有一种方法可以从控制器那里做到这一点。
答案 0 :(得分:8)
您的ViewBag.PayList
已经是SelectList
的类型。我没有理由创建SelectList
两次,所以不应该只是:
@Html.DropDownList("Pay", ViewBag.PayList)
或
@Html.DropDownList("Pay", ViewBag.PayList as SelectList)
(我从不使用ViewBag,所以我不确定你的版本是否是强类型的。)
答案 1 :(得分:6)
只需使用
@Html.DropDownList("Pay", ViewBag.PayList)
在你看来
答案 2 :(得分:0)
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="lblStatus_@data.TempId" destinationID = "@data.Destination">@data.Destinationvalue</span>
<select class="status" id="ddlStatus_@data.TempId">
<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="lblSource_@data.TempId" >@data.Source</span>
<input class="Source" id="txtSource_@data.TempId" type="text" value="@data.Source" maxlength="30" />
</td>
<td>
<table style="width: 50%;">
<tr>
<td>
<input class="edit" id="Edit_@data.TempId" type="button" value="Edit" />
<input class="update" id="Update_@data.TempId" type="button" value="Update" />
</td>
<td class="Gcancle" id="Canc_@data.TempId">
<input class="gridcancel" id="Cancel_@data.TempId" type="button" value="Cancel" />
</td>
</tr>
</table>
</td>
<td>
<table style="width: 50%;">
<tr>
<td>
<input class="delete" id="Delete_@data.TempId" 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>