2013-11-22 18 views
0

我無法找到並返回正確視圖的操作。當模型是字符串時,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視圖?

回答

1

當您傳遞字符串作爲唯一參數時,它使用的是過載View(string viewName)

您需要確保使用過載View(object model)

您可以將模型轉換爲object

return View((object)id); 

我傾向於避免string模型,專門爲此原因。

確保你看看所有的重載View - 例如:string, stringviewName, masterName其中string, objectviewName, model,所以爲了使用,你會不得不再次轉換爲object。三個參數的過載是viewName, masterName, model

+0

由於跌落下面的代碼!想不到如何強制它使用對象之一,非常簡單的解決方案。 – Kyle

+0

是的,我第一次使用了一個字符串模型時,引起了我的警惕,讓我瘋了。 –

0

默認情況下,WebFormViewEngine包含在RazorViewEngine之前。這就是爲什麼它尋找* .aspx。

爲了解決這個問題,告訴搜索引擎的WebForm意見前尋找剃刀查看在您的Global.asax.cs

protected void Application_Start() 
{ 
    ViewEngines.Engines.Clear(); 
    ViewEngines.Engines.Add(new RazorViewEngine()); 
    ViewEngines.Engines.Add(new WebFormViewEngine()); 
} 
+0

雖然這確實會改變它,所以它首先查找.cshtml,然後是.aspx,但它仍然在相同的位置查找每個位置,並且.cshtml不是正確的位置。 – Kyle

相關問題