I recently hit an issue with ASP.NET MVC display templates. Say this is my model:
public class Model
{
public int ID { get; set; }
public string Name { get; set; }
}
this is the controller:
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new Model());
}
}
and this is my view:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<DisplayTemplateWoes.Models.Model>" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title>Index</title>
</head>
<body>
<div>
<%: Html.DisplayForModel() %>
</div>
</body>
</html>
If I need for some reason a display template for all strings I will create a String.ascx partial view like this:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" %>
<%: Model %> (<%: Model.Length %>)
And here is the problem - at runtime the following exception is thrown: "The model item passed into the dictionary is of type 'System.Int32', but this dictionary requires a model item of type 'System.String'"
It seems that String.ascx is used for both the integer and string property of the Model class. I expected it to be used only for the string property - after all it is named String.ascx not Object.ascx or Int32.ascx.
Is this by design? If yes - is it documented somewhere? If not - can it be considered a bug?