EditorFor不适用于派生类型 [英] EditorFor isn't working on derived type

查看:102
本文介绍了EditorFor不适用于派生类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

至少,我认为这与问题有关.我的情况是这样的:

At least, I think that's related to the problem. My scenario is this:

我有许多具有公共字段的业务实体,每个业务实体都有该实体唯一的自定义字段.因此,在代码中,此模型被建模为EntityBase类,并且有许多由此类派生的类,例如Derived.

I've got a number of business entities with common fields, and each one has custom fields unique to that entity. So in code, this is modeled as an EntityBase class, and there are a number of classes derived from this, e.g., Derived.

要创建可重用的UI,我有一个名为EntityBase.vbhtml的视图,如下所示:

To make a reusable UI, I've got a view called EntityBase.vbhtml that looks like this:

@ModelType EntityBase

@Using Html.BeginForm("Edit", Model.GetType.Name)
    @* show the editor template for the derived type *@
    @* !!the next line renders nothing!! *@
    @Html.EditorFor(Function(x) Model, Model.GetType.Name)

    [show a bunch of stuff common to all EntityBase objects]
End Using

,然后对执行此操作的派生类调用Derived.vbhtml:

and then one called Derived.vbhtml for the derived classes that does this:

@ModelType Derived
[show an EditorFor for various Derived-specific fields]

然后,当您导航到\Derived\Edit\123时,它将返回默认视图Derived\Edit.vbhtml,该视图将执行以下操作:

Then, when you navigate to \Derived\Edit\123, it returns the default view Derived\Edit.vbhtml, which just does this:

@Html.EditorForModel("EntityBase")

这样,控制器仅返回预期的默认Edit视图,这是对EntityBase视图的单线调用,它执行其工作并调用Derived来呈现派生类的内容,而该类没有

In this way, controllers just return the expected default Edit view, which is a one-liner call to the EntityBase view, which does its thing and invokes the Derived to render the derived class stuff that it has no knowledge of.

我认为这没什么大不了的,但是那没用.如视图代码中所标记,当我在基类视图中调用EditorForModel时,指定派生类的名称以用作模板,它不会呈现任何内容.我已经测试过,如果我在顶层编辑"模板中调用此完全相同的代码行,则可以正常工作.因此,关于MVC的继承有一些不喜欢的东西,但是我看不到.请帮忙!

I thought this was unremarkable, but it doesn't work. As marked in the view code, when I call EditorForModel within the base class view, specifying the name of the derived class for use as a template, it doesn't render anything. I've tested that if I call this exact same line of code in the top level Edit template, it works fine. So there's something about the inheritance that MVC doesn't like, but I can't see what. Please help!

更新:如果我使用的是Partial而不是EditorFor(并将相应的模板从EditorTemplates文件夹移至Shared文件夹),则它可以像我期望的那样工作,但这不是一个很好的选择解决方案,因为我认为不遵循EditorFor模板的命名约定会令人困惑.

Update: It works as I would expect if instead of EditorFor I use Partial (and move the corresponding template to the Shared folder from the EditorTemplates folder), but that's not a great solution because I think it's confusing not to follow the naming convention for EditorFor templates.

推荐答案

在这种情况下,虽然MVC不会找到命名模板,但是如果您指定模板的完整路径,它将找到该模板.因此,我没有进一步战斗,而是实现了以下帮助器功能:

It appears that, while MVC won't locate the named template in this circumstance, it will find it if you specify the full path to the template. So, rather than fight this any further, I implemented the following helper function:

<Extension()> _
Public Function EditorForObject(Of T, TValue)(ByVal htmlHelper As HtmlHelper(Of T), ByVal obj As TValue) As IHtmlString
    Dim sTemplateName = "~/Views/Shared/EditorTemplates/" & obj.GetType.Name & ".vbhtml"

    'Return htmlHelper.EditorFor(Function(x) obj) <-- this should work but doesn't
    Return htmlHelper.Partial(sTemplateName, obj)
End Function

在英语中,这意味着:向对象询问其类型名称,形成该类型的编辑器模板的显式路径,然后调用HtmlHelper.Partial,指定对象和模板的完整路径.我敢肯定,这可能会更通用(并且不会针对vb进行硬编码),但是它可以正常工作.

In English, this means: ask the object for its type name, form the explicit path to the editor template for that type, and then invoke HtmlHelper.Partial, specifying the object and the full path to the template. I'm sure this could be more general (and not hardcoded for vb), but it works.

然后用法如下:

@Html.EditorForObject(Model)

实际上,这比我想做的要好得多,这更加混乱:

and actually, this is even better than what I was trying to do, which is much messier:

@Html.EditorFor(Function(x) Model, Model.GetType.Name)

即使没有模板查找问题,这也很方便,因为能够传递对象以进行编辑(或显示),而不是仅返回该对象的虚拟lambda,非常方便.

Even without the template lookup problem, this would be handy, because it's convenient to be able to pass an object for editing (or display), rather than a dummy lambda that just returns that object.

仍然,我认为查找问题一定是MVC中的错误. (如果有时间,我想我可以检查源代码.)有人可以对此进行确认或评论吗?

Still, I think the lookup problem must be a bug in MVC. (If I ever get time, I guess I can check the source code.) Can anyone confirm or comment on this?

这篇关于EditorFor不适用于派生类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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