強く型付けされたビューのコンパイル時エラーが発生する可能性がある方法はありますか?/Views/Home/Index.cshtml
次のコードと強く型付けされたモデルを含むビューがフォルダーにあるとします。
@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);
}
}
ご覧のとおり、View()はオブジェクトをモデルとして受け入れるため、コンパイラーはモデルが無効であると文句を言わず、代わりに実行時エラーを出力します。これは次のとおりです。
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'.
このようなモデルタイプの不一致の場合に実行時エラーの代わりにコンパイル時エラーを取得できる方法はありますか、またはこれに対する回避策はありますか?