我们在Webgrid中有dropdownlistfor,我正在尝试在Webgrid中绑定dropdownlistfor。我第一次绑定没有任何问题,因为它在下拉列表中显示了正确的值。 第二次,它没有在下拉列表中绑定正确的值并显示错误的selectedvalue。
webgrid中的下拉列表:
WebGrid grid = new WebGrid(rowsPerPage: ReviewsController.LookupPageSize, ajaxUpdateContainerId: "dvSearchResultsGrid", ajaxUpdateCallback: "OnCompleteAjaxFormReviews");
grid.Bind(Model.Users, autoSortAndPage: true, rowCount: @Model.TotalCount);
var gridColumns = new List<WebGridColumn>();
gridColumns.Add(grid.Column("Name", "Name"));
gridColumns.Add(grid.Column("SourcingCompany", "Sourcing company"));
gridColumns.Add(grid.Column("SponsorName", "Sponsor name"));
gridColumns.Add(grid.Column("CurrentNetworkAccessLevel", "Network Access Level",
format: @<text>@Html.DropDownListFor(model => model.NetworkAccessLevel.SelectedValue, new SelectList(Model.NetworkAccessLevel.Items , "Key", "Value", Model.NetworkAccessLevel.SelectedValue = @item.CurrentNetworkAccessLevel), new { @id = "ddlNetworkAccessValue", @class = "dropdownlist", style = "width:70px" })
<input type="hidden" id="hdnNetworkAccessLevel" value="@item.CurrentNetworkAccessLevel" />
<a class="NetworkAccessLevelReset" href="javascript:void(0)">Reset</a>
</text>, style: "columnnowrap12"));
@grid.GetHtml(tableStyle: "table table-striped table-hover",
mode: WebGridPagerModes.All,
firstText: "<< First",
previousText: "< Prev",
nextText: "Next >",
lastText: "Last >>", numericLinksCount: 9,
htmlAttributes: new { id = "Grid" },
columns: grid.Columns(gridColumns.ToArray()))
}
控制器部分:(单击按钮可获取网格数据并绑定到网格):
[HttpPost]
public ActionResult DisplayAll(ReviewsListViewModel model)
{
// To set the Network Access Level dropdownlist
NetworkAccessLevelItems(model);
model.IsAllUsers = true;
model.IsUsersExpiringWithin35Days = false;
List<UserBase> users = GetSearchResults(model);
if (users != null && users.Count() > 0)
{
if (users.Count() >= 1000)
{
model.ErrorMsg = ErrorCodeHelper.GetInstance().FindByCode("S102");
}
else
{
model.TotalCount = users.Count();
model.Users = users;
}
}
else
{
model.ErrorMsg = ErrorCodeHelper.GetInstance().FindByCode("S101");
}
return PartialView("~/Views/Reviews/_Results.cshtml", model);
}
视图:(具有部分视图的主视图的结构)
<div id="dvReviews">
@using (Ajax.BeginForm("", "", new AjaxOptions { HttpMethod = "", UpdateTargetId = "dvGridSearchResults", OnSuccess = "OnCompleteAjaxFormReviews()", LoadingElementId = "progress" }))
{
@Html.AntiForgeryToken()
<fieldset>
<div id="dvTitle"><h2> @Html.DisplayFor(model => model.Title) </h2></div>
<div class="container-fluid">
<div class="row">
<div class="col-sm-12 col-md-12 col-lg-12">
@Html.Partial("_Instructions", Model)
@Html.Partial("_Filter", Model)
<div id="dvGridSearchResults">
@Html.Partial("_Results", Model)
</div>
</div>
</div>
</div>
</fieldset>
}
<div id="progress" class="loader" style="display:none">
</div>
</div>