为什么在我调用EditorForModel时会忽略我的编辑器模板? [英] Why is my editor template ignored when I call EditorForModel?

查看:50
本文介绍了为什么在我调用EditorForModel时会忽略我的编辑器模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 LocalizedString 的类,该类在asp.net mvc 3项目中引用的外部库中定义.

I have a class called LocalizedString that is defined in an external library that is referenced in my asp.net mvc 3 project.

我已经在〜\ View \ Shared \ EditorTemplates 文件夹中创建了一个名为 LocalizedString.cshtml 的编辑器模板.

I have created an editor template called LocalizedString.cshtml in the ~\View\Shared\EditorTemplates folder.

我有以下型号

public class Region
{
    public LocalizedString Title { get; set; }
}

我有以下测试页:

@model Region

@Html.EditorForModel()
@Html.EditorFor(x => x.Title)

当我调用 EditorForModel 时不会调用 LocalizedString 的编辑器模板,但是当我显式调用 EditorFor(x => x.x时,它会渲染.),所以我知道我没有推荐问题.

The editor template for LocalizedString is not invoked when I call EditorForModel, but it does render when i explicitly call EditorFor(x => x.Title) so I know that I don't have referral problems.

为什么当我调用 EditorForModel (或等效的 EditorFor(x => x))时,我的编辑器模板被忽略

Why is my editor template ignored when I invoke EditorForModel (or its equivalent EditorFor(x => x))

更新

我创建了一个新项目来重现此行为.我只是使用默认的ASP.NET MVC 3 Internet应用程序.

I created a new project to reproduce this behavior. I just used the default ASP.NET MVC 3 Internet Application.

Index.cshtml

@model MvcApplication1.Models.Region
@{
    ViewBag.Title = "Home Page";
}

<h2>@ViewBag.Message</h2>
<p>
    To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
</p>

@Html.EditorForModel()

HomeController.cs

using System.Web.Mvc;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";

            return View(new Region());
        }
    }
}

Region.cs

namespace MvcApplication1.Models
{
    public class Region
    {
        public Region()
        {
            this.Name = "RegionInstance";
            this.Title = new LocalizedString();
        }

        public string Name { get; set; }

        public LocalizedString Title { get; set; }
    }
}

LocalizedString.cs

namespace MvcApplication1.Models
{
    public class LocalizedString
    {
        public LocalizedString()
        {
            this.Name = "LocalizedStringInstance";
        }

        public string Name { get; set; }
    }
}

如果运行此程序,则输出将仅显示用于输入区域名称的文本.永远不会输出 LocalizedString.cshtml 内容.

If you run this program, the output will only display a text input for the name of the region. The LocalizedString.cshtml content is never output.

我还尝试在 Region.Title 属性上使用 UIHint ,但是它仍然不显示.

I also tried to use UIHint over the Region.Title property, but it still does not display.

推荐答案

原来,这与默认情况下,ASP.NET MVC不会像我通过反射发现的那样呈现子复杂类型

By default, ASP.NET MVC does not render child complex type as I found out through reflection

private static bool ShouldShow(ModelMetadata metadata, TemplateInfo templateInfo)
{
  if (metadata.ShowForEdit && metadata.ModelType != typeof (EntityState) && !metadata.IsComplexType)
    return !templateInfo.Visited(metadata);
  else
    return false;
}

System.Web.Mvc.Html.DefaultEditorTemplates 中的 ObjectTemplate 调用.

检查 IsComplexType 的行为后,我发现如果不能通过 TypeConverter 将其转换为字符串,则该类型是复杂的.如果我为我的子模型创建一个TypeConverter,它将渲染.尽管此解决方案并不理想,但我可能会坚持使用 EditorFor 而不是 EditorForModel .

After checking the behavior of IsComplexType, I found out that a type is complex if it cannot be converted to a string through a TypeConverter. If I create a TypeConverter for my child model, it does render. Although this solution is not ideal and I might just stick with EditorFor instead of EditorForModel.

using System;
using System.ComponentModel;

public class LocalizedStringTypeConverter : TypeConverter
{
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        if (sourceType == typeof(string))
        {
            return true;
        }

        return base.CanConvertFrom(context, sourceType);
    }
}

这篇关于为什么在我调用EditorForModel时会忽略我的编辑器模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆