ASP.NET MVC Display Template for strings is used for integers

不问归期 提交于 2019-12-04 22:52:45

问题


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?


回答1:


This seem to be by design. You will have to make string template more general. String template works as default template for every non-complex model that doesn't have it's own template.

Default template for string (FormattedModelValue is object):

internal static string StringTemplate(HtmlHelper html) {
    return html.Encode(html.ViewContext.ViewData.TemplateInfo.FormattedModelValue);
}

Template selection looks like this:

foreach (string templateHint in templateHints.Where(s => !String.IsNullOrEmpty(s))) {
    yield return templateHint;
}

// We don't want to search for Nullable<T>, we want to search for T (which should handle both T and Nullable<T>)
Type fieldType = Nullable.GetUnderlyingType(metadata.RealModelType) ?? metadata.RealModelType;

// TODO: Make better string names for 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 you want to create template only for string, you should do it like this (String.ascx):

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<object>" %>

<% var model = Model as string; %>
<% if (model != null) { %>
    <%: model %> (<%: model.Length %>)
<% } else { %>
    <%: Model %>
<% } %>


来源:https://stackoverflow.com/questions/5038858/asp-net-mvc-display-template-for-strings-is-used-for-integers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!