字符串ASP.NET MVC显示模板用于整数 [英] ASP.NET MVC Display Template for strings is used for integers
问题描述
我最近打的ASP.NET MVC显示模板的问题。之所以这样说,是我的模型:
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; }
}
这是控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new Model());
}
}
这是我的看法:
<%@ 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>
如果我需要某种原因,所有的字符串,显示模板,我会创建一个这样的String.ascx局部视图:
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 %>)
和这里的问题是 - 在运行时以下异常被抛出:
的传递到字典中的模型产品类型'System.Int32',但这需要字典类型的模型项目'System.String'的
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'"
似乎String.ascx用于两个示范类的整数和字符串属性。我预期只为字符串属性使用 - 毕竟它被命名为String.ascx不Object.ascx或Int32.ascx。
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?
推荐答案
这似乎是由设计。你将不得不作出的字符串模板比较一般。字符串模板工程作为默认模板为每个非复杂的模型不具有它自己的模板。
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.
对于字符串默认模板(FormattedModelValue为对象):
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;
}
}
所以,如果你想创建仅字符串模板,你应该这样做这样的(String.ascx):
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 %>
<% } %>
这篇关于字符串ASP.NET MVC显示模板用于整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!