{"id":3715,"date":"2014-03-29T08:26:15","date_gmt":"2014-03-29T08:26:15","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/29\/compile-time-errors-for-incorrect-model-in-asp-net-mvc-collection-of-common-programming-errors\/"},"modified":"2014-03-29T08:26:15","modified_gmt":"2014-03-29T08:26:15","slug":"compile-time-errors-for-incorrect-model-in-asp-net-mvc-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/29\/compile-time-errors-for-incorrect-model-in-asp-net-mvc-collection-of-common-programming-errors\/","title":{"rendered":"Compile time errors for incorrect model in ASP.Net MVC-Collection of common programming errors"},"content":{"rendered":"<p>Is there any way where one can have compile time errors for strongly typed views. Let&#8217;s say I have a view in the folder <code>\/Views\/Home\/Index.cshtml<\/code> with the following code &amp; strongly typed Model:<\/p>\n<pre><code>@model CSTemplate.Models.Home.HomeIndexModel\n\n@{\n    ViewBag.Title = \"Index\";\n}\n\n<\/code><\/pre>\n<h2><code>Index<\/code><\/h2>\n<pre>\n<\/pre>\n<p><code>and then the controller, located at <code>\/Controllers\/HomeController.cs<\/code> would return the following code.<\/code><\/p>\n<pre><code>public class HomeController : Controller\n    {\n        \/\/\n        \/\/ GET: \/Home\/\n\n        public ActionResult Index()\n        {\n            List s = new List();\n            return View(s);\n        }\n\n    }\n<\/code><\/pre>\n<p>As you can see, since the View() accepts an object as a model, the compiler won&#8217;t complain that the model is invalid and would output a run-time error instead, which is:<\/p>\n<pre><code>Server Error in '\/' Application.\n\nThe 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'.\n<\/code><\/pre>\n<p>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?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Is there any way where one can have compile time errors for strongly typed views. Let&#8217;s say I have a view in the folder \/Views\/Home\/Index.cshtml with the following code &amp; strongly typed Model: @model CSTemplate.Models.Home.HomeIndexModel @{ ViewBag.Title = &#8220;Index&#8221;; } Index and then the controller, located at \/Controllers\/HomeController.cs would return the following code. public class [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3715","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3715","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3715"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3715\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3715"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3715"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3715"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}