有沒有什麼辦法可以讓強類型視圖的編譯時錯誤。比方說,我有一個觀點的文件夾中/Views/Home/Index.cshtml
用下面的代碼&強類型型號:在ASP.Net中編譯錯誤模型的時間錯誤MVC
@model CSTemplate.Models.Home.HomeIndexModel
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
,然後控制器,位於/Controllers/HomeController.cs
將返回下面的代碼。
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
List<string> s = new List<string>();
return View(s);
}
}
正如你所看到的,因爲視圖()接受一個對象作爲模型,編譯器不會抱怨,該模型是無效的,並會輸出一個運行時錯誤,而不是,這就是:
Server Error in '/' Application.
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[System.String]', but this dictionary requires a model item of type 'CSTemplate.Models.Home.HomeIndexModel'.
有什麼辦法可以讓我編譯時錯誤,而不是運行時錯誤的情況下,這種模型類型不匹配,或有任何解決方法呢?
如果你有ReSharper的安裝時它會通知你,如果類型不匹配 –