MVC @Html.下拉列表在ViewBag中使用SelectList时出现错误
本文关键字:SelectList 错误 下拉列表 @Html ViewBag MVC | 更新日期: 2023-09-27 18:14:13
我在控制器中创建了一个列表:
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"))
当我试图显示它时,它显示如下:数据绑定:"System.Web.Mvc。SelectListItem'不包含名称为'Id'的属性。不知道为什么这不起作用。
还有,我如何默认选择列表的值。我喜欢默认为"是"。我认为有一种方法可以从控制器做到这一点
您的ViewBag.PayList
已经是SelectList
的类型。我不认为有理由创建两次SelectList
,所以它不应该只是:
@Html.DropDownList("Pay", ViewBag.PayList)
或
@Html.DropDownList("Pay", ViewBag.PayList as SelectList)
(我从来没有使用过ViewBag,所以我不确定你的版本是否是强类型的)。
就用
@Html.DropDownList("Pay", ViewBag.PayList)
在你的视图
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>