Compile time errors for incorrect model in ASP.Net MVC-Collection of common programming errors
Is there any way where one can have compile time errors for strongly typed views. Let’s say I have a view in the folder /Views/Home/Index.cshtml
with the following code & strongly typed Model:
@model CSTemplate.Models.Home.HomeIndexModel
@{
ViewBag.Title = "Index";
}
Index
and then the controller, located at
/Controllers/HomeController.cs
would return the following code.
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
List s = new List();
return View(s);
}
}
As you can see, since the View() accepts an object as a model, the compiler won’t complain that the model is invalid and would output a run-time error instead, which is:
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'.
Is there any way where I can get compile time errors instead of run time errors in case of such model type mismatch, or is there any workaround for this?