Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC Display Template for strings is used for integers
    text
    copied!<p>I recently hit an issue with ASP.NET MVC display templates. Say this is my model:</p> <pre><code>public class Model { public int ID { get; set; } public string Name { get; set; } } </code></pre> <p>this is the controller:</p> <pre><code>public class HomeController : Controller { public ActionResult Index() { return View(new Model()); } } </code></pre> <p>and this is my view:</p> <pre><code>&lt;%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage&lt;DisplayTemplateWoes.Models.Model&gt;" %&gt; &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head runat="server"&gt; &lt;title&gt;Index&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;div&gt; &lt;%: Html.DisplayForModel() %&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>If I need for some reason a display template for all strings I will create a String.ascx partial view like this:</p> <pre><code>&lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&lt;string&gt;" %&gt; &lt;%: Model %&gt; (&lt;%: Model.Length %&gt;) </code></pre> <p>And here is the problem - at runtime the following exception is thrown: <em>"The model item passed into the dictionary is of type 'System.Int32', but this dictionary requires a model item of type 'System.String'"</em></p> <p>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. </p> <p>Is this by design? If yes - is it documented somewhere? If not - can it be considered a bug?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload