此問題已被要求提供類似問題,但我不知道如何將這些答案應用於我的特定問題:沒有類型爲'IEnumerable <SelectListItem>'的ViewData項目的密鑰'RouterElect'
視圖模型
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);
}
另一個問題是問奧凱行動。它調用changeCurrentRouter腳本,該腳本執行ChangeCurrrentRouter行動:
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);
}
Model.JPortalChamberUsersAsItems的後端代碼是什麼? –
單擊「確定」按鈕時會發生什麼?它是否提交表單?採取哪種行動方法?你能分享行動方法的代碼嗎? – Shyju