这个问题之前曾被问过类似的问题,但我不确定如何将这些答案应用于我的具体问题:
视图模型
public string RouterElect { get; set; }
查看
if ((!string.IsNullOrWhiteSpace(Model.JudgeCode)) && (Model.userIsAuthorized))
{
<div id="ChangeRouterSection" class="live-tile" data-mode="carousel" data-direction="vertical" data-delay="7000">
@{Html.RenderPartial("_ChangeCurrentRouter");}
</div>
}
else
{
<div id="ChangeRouterSection" class="live-tile" data-mode="carousel" data-direction="vertical" data-delay="7000">
<div class="tile">
User not authorized. Access denied.
</div>
</div>
}
局部视图
@model JudicialPortal.ViewModels.PortalIndexView
<div class="tile">
Change Current Router
@Html.DropDownListFor(x => x.RouterElect, Model.JPortalChamberUsersAsItems, new { @id = "changeCurrentRouterInput", @class = "m-wrap", @onchange = "" })
<button type="button" class="m-btn green-stripe" onclick="changeCurrentRouter();"
style="color: #444; text-decoration: none;">
Okay</button>
</div>
页面加载时渲染没有问题,如下所示:
但是,当选择下拉列表中的项目然后单击“确定”按钮时,会发生以下错误:
不存在具有键“RouterElect”的“IEnumerable”类型的 ViewData 项。
对于代码行:
@Html.DropDownListFor(x => x.RouterElect, Model.JPortalChamberUsersAsItems, new { @id = "changeCurrentRouterInput", @class = "m-wrap", @onchange = "" })
任何想法都值得赞赏。
更新: 有人问JPortalChamberUsersAsItems后端代码:
JPortalChamberUsersAsItems = new List<SelectListItem>();
if (HttpContext.Current.Session["JPortalChamberUsersAsItems"] != null)
{
JPortalChamberUsersAsItems = HttpContext.Current.Session["JPortalChamberUsersAsItems"] as List<SelectListItem>;
}
else
{
for (int i = 0; i < tempArrayOfUsers.Count; i++)
{
JPortalChamberUsersAsItems.Add(new SelectListItem { Text = tempArrayOfUsers[i], Value = tempArrayOfUsers[i] });
}
foreach (SelectListItem routerUser in JPortalChamberUsersAsItems)
{
if (!string.IsNullOrWhiteSpace(routerUser.Text))
{
if (routerUser.Text.ToUpper() != "PLEASE SELECT A ROUTER")
{
routerUser.Text = ReplaceUsernameWithFullName(routerUser.Text);
}
}
}
JPortalChamberUsersAsItems.Insert(0, (new SelectListItem { Text = "Please select a router", Value = "Please select a router" })); //2014-12-15 This code adds the "default" first option - DO NOT REMOVE
HttpContext.Current.Session.Add("JPortalChamberUsersAsItems", JPortalChamberUsersAsItems);
}
还有一个关于 OKay 行动的问题。 它调用changeCurrentRouter脚本,该脚本执行ChangeCurrentRouter操作:
public ActionResult ChangeCurrentRouter(string changeCurrentRouterInput, string JudgeCode, bool userIsAuthorized, PortalIndexView viewmodel)
{
bool databaseCallOk = false;
string errorString = "";
viewmodel.userIsAuthorized = userIsAuthorized;
viewmodel.JudgeCode = JudgeCode;
if (!string.IsNullOrWhiteSpace(changeCurrentRouterInput))
{
if (changeCurrentRouterInput.ToUpper() != "PLEASE SELECT A ROUTER")
{
//database call
databaseCallOk = viewmodel.changeRouterUser(changeCurrentRouterInput, JudgeCode);
}
}
if (databaseCallOk == false)
{
if (string.IsNullOrWhiteSpace(viewmodel.errorCatcher))
{
errorString = "An error occurred trying to change the router for your chambers. Please confirm your selection and try again.";
viewmodel.errorCatcher = errorString;
}
}
viewmodel.justAttemptedRoutingChange = true;
return PartialView("~/Views/Shared/_ChangeCurrentRouter.cshtml", viewmodel);
}
当您单击“确定”按钮时,我不确定您在做什么。我假设它向操作方法提交表单,并且您在某些用例中再次将发布的模型返回到视图(可能是在模型验证失败时?)。每当您将对象返回到视图时,您都需要确保再次(重新)加载
JPortalChanerUsersAsItems
集合属性。因为 Http 是无状态的,它不会在多个请求之间保留该属性的值。
[HttpPost]
public ActionResult SomeAction(PortalIndexView model)
{
if(ModelState.IsValid)
{
// to do : Save and redirect
}
// Reload the collection.
// I hard coded 2 items here. But you may get it from db and use it
model.JPortalChamberUsersAsItems = new List<SelectListItem> {
new SelectListItem { Value="1", Text="Detroit" } ,
new SelectListItem { Value="2", Text="Ann Arbor" }
};
return View(model);
}