我無法找到並返回正確視圖的操作。當模型是字符串時,MVC無法找到正確的視圖
這是在Account
區域,在AccountController
,並且視圖是ConfirmAccount.cshtml.
它需要一個型號string
。
我嘗試了這些:
A)我想只是在模型傳遞正常人一樣,與相匹配的操作名稱視圖名稱。
public ActionResult ConfirmAccount(string id)
{
return View(id);
}
這似乎不起作用,因爲模型是一個字符串,它只是試圖找到一個名稱爲id的字符串。
B)所以我試圖指定視圖名稱,希望它能找到ConfirmAccount視圖,並理解id是模型。
public ActionResult ConfirmAccount(string id)
{
return View("ConfirmAccount", id);
}
這一結果我感到困惑,它試圖找到以下內容:
〜/地區/客戶/瀏覽/帳號/ ConfirmAccount.aspx 〜/地區/客戶/瀏覽/ Account/ConfirmAccount.ascx 〜/ Areas/Account/Views/Shared/ConfirmAccount.aspx 〜/ Areas/Account/Views/Shared/ConfirmAccount.ascx 〜/ Views/Account/ConfirmAccount.aspx 〜/ Views/Account/ConfirmAccount.ascx〜/ Views/Shared/ConfirmAccount.aspx 〜/ Views/Shared/ConfirmAccount.ascx 〜/地區/帳戶/查看/帳戶/ xNMf1HNl_2pLp-6MXpXl3g2If.master 〜/地區/帳戶/查看/共享/ xNMf1HNl_2pLp-6MXpXl3g2If.master 〜/查看/帳戶/ xNMf1HNl_2pLp-6MXpXl3g2If.master 〜/查看/共享/xNMf1HNl_2pLp-6MXpXl3g2If.master 〜/地區/帳戶/查看/帳戶/ xNMf1HNl_2pLp-6MXpXl3g2If.cshtml 〜/地區/帳戶/查看/帳戶/ xNMf1HNl_2pLp-6MXpXl3g2If.vbhtml 〜/地區/帳戶/查看/共享/ xNMf1HNl_2pLp -6MXpXl3g2If.cshtml 〜/地區/帳戶/查看/共享/ xNMf1HNl_2pLp-6MXpXl3g2If.vbhtml 〜/查看/帳戶/ xNMf1HNl_2pLp-6MXpXl3g2If.cshtml 〜/查看/帳戶/ xNMf1HNl_2pLp-6MXpXl3g2If.vbhtml 〜/查看/共享/xNMf1HNl_2pLp-6MXpXl3g2If.cshtml 〜/ Views/S hared/xNMf1HNl_2pLp-6MXpXl3g2If.vbhtml
凡在尋找的ASPX/ascx的頁面將是完美的,如果它一直在尋找CSHTML頁面出現,但由於某種原因它不是,而是試圖找到CS/vbshtml頁面由id。有誰知道爲什麼會發生這種情況? C)第三個也是唯一我能想到的其他事情是指定控制器名稱。
public ActionResult ConfirmAccount(string id)
{
return View("ConfirmAccount", "Account", id);
}
〜/地區/帳戶/查看/帳戶/ ConfirmAccount.aspx 〜/地區/帳戶/查看/帳戶/ ConfirmAccount.ascx 〜/地區/帳戶/查看/共享/ ConfirmAccount.aspx 〜/ Areas/Account/Views/Shared/ConfirmAccount.ascx 〜/ Views/Account/ConfirmAccount.aspx 〜/ Views/Account/ConfirmAccount.ascx〜/ Views/Shared/ConfirmAccount.aspx 〜/ Views/Shared/ConfirmAccount.ascx 〜/地區/帳戶/查看/帳戶/帳戶。大師 〜/地區/帳戶/查看/共享/帳戶管理 〜/ Views/Account/Account.master〜/ Views/Shared/Account.master 〜/ Areas/Account/Views/Account/Account.cshtml 〜 /Areas/Account/Views/Account/Account.vbhtml 〜/ Areas/Account/Views/Shared/Account.cshtml 〜/ Areas/Account/Views/Shared/Account.vbhtml 〜/ Views/Account/Account.cshtml 〜/查看/帳號/ Account.vbhtml 〜/查看/共享/ Account.cshtml〜/查看/共享/ Account.vbhtml
這類似於最後一個,其中第一批是在正確的道路如果它在那裏尋找cshtml,但由於某種原因,它不是,我也不是理解爲什麼。在尋找cshtml頁面的地方,它會查找帳戶而不是ConfirmAccount。
如何在我的模型是字符串的場景中正確返回ConfirmAccount.cshtml視圖?
由於跌落下面的代碼!想不到如何強制它使用對象之一,非常簡單的解決方案。 – Kyle
是的,我第一次使用了一個字符串模型時,引起了我的警惕,讓我瘋了。 –