ASP.NET MVC Display Template for strings is used for integers -
i hit issue asp.net mvc display templates. model:
public class model { public int id { get; set; } public string name { get; set; } }
this controller:
public class homecontroller : controller { public actionresult index() { return view(new model()); } }
and 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 need reason display template strings create string.ascx partial view this:
<%@ control language="c#" inherits="system.web.mvc.viewusercontrol<string>" %> <%: model %> (<%: model.length %>)
and here problem - @ runtime following exception thrown: "the model item passed dictionary of type 'system.int32', dictionary requires model item of type 'system.string'"
it seems string.ascx used both integer , string property of model class. expected used string property - after named string.ascx not object.ascx or int32.ascx.
is design? if yes - documented somewhere? if not - can considered bug?
this seem design. have make string template more general. string template works default template every non-complex model doesn't have it's own template.
default template string (formattedmodelvalue object):
internal static string stringtemplate(htmlhelper html) { return html.encode(html.viewcontext.viewdata.templateinfo.formattedmodelvalue); }
template selection looks this:
foreach (string templatehint in templatehints.where(s => !string.isnullorempty(s))) { yield return templatehint; } // don't want search nullable<t>, want search t (which should handle both t , nullable<t>) type fieldtype = nullable.getunderlyingtype(metadata.realmodeltype) ?? metadata.realmodeltype; // todo: make better string names generic types yield return fieldtype.name; if (!metadata.iscomplextype) { yield return "string"; } else if (fieldtype.isinterface) { if (typeof(ienumerable).isassignablefrom(fieldtype)) { yield return "collection"; } yield return "object"; } else { bool isenumerable = typeof(ienumerable).isassignablefrom(fieldtype); while (true) { fieldtype = fieldtype.basetype; if (fieldtype == null) break; if (isenumerable && fieldtype == typeof(object)) { yield return "collection"; } yield return fieldtype.name; } }
so if want create template string, should (string.ascx):
<%@ control language="c#" inherits="system.web.mvc.viewusercontrol<object>" %> <% var model = model string; %> <% if (model != null) { %> <%: model %> (<%: model.length %>) <% } else { %> <%: model %> <% } %>
Comments
Post a Comment