强制EditorFor至preFIX输入项目上的看法与类名? [英] Forcing EditorFor to prefix input items on view with Class Name?

查看:143
本文介绍了强制EditorFor至preFIX输入项目上的看法与类名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个EditorFor:

I have an EditorFor:

<%: Html.EditorFor(model => model.Client, "ClientTemplate", new { editing = false })%>

这将绑定下来到视图罚款(如预期),但是当模型被贴不会绑定绑定回来。
这是因为表单ID的不是prefixed与客户。

This will bind coming down to the view fine (as expected) but will not bind bind back when the model gets posted. This is due to the form id's not being prefixed with "Client."

一般在这种情况下,我只是通过在模型,然后结合在模板的输入model.Client.PropertyName但这不是作为模板是在两个不同的ViewModels(具有客户端上),用于在这种情况下,一个选择

Usually in this situation i just pass in model and then bind the inputs to model.Client.PropertyName in the Template but this is not an option in this case as the template is used on two different viewmodels (that have client on).

在得到这个任何建议,以正确绑定?

Any suggestions on getting this to bind properly?

非常感谢,
可汗。

Many thanks, Kohan.

看来,这是我的一个误会,因为我现在明白它的问题是,fluentHtml不EditorFor模板内工作。 (这同样适用此修复程序,事实证明没有必要为EditorFor如果我正常的MVC HTML辅助更换fluentHtml会自动preFIX对我来说这)

It seems this was a misunderstanding on my part, the issue as i now understand it is that fluentHtml does not work inside EditorFor Templates. (The same goes for this fix, which as it turns out was not needed as EditorFor will prefix for me automatically if i replace the fluentHtml with normal mvc html helpers)

推荐答案

尝试是这样的:

<% Html.BeginHtmlFieldPrefixScope("Client") {
  Html.EditorFor(model => model.Client, "ClientTemplate", new { editing = false });
<% } %>

你与EditorFor,LabelFor和喜欢每场将是prefixed。

Every field you make with EditorFor, LabelFor and the likes will be prefixed.

编辑:
这里的扩展方法我用了,不好意思!

Here's the extension method I'm using, sorry!

public static IDisposable BeginHtmlFieldPrefixScope(this HtmlHelper html, string htmlFieldPrefix)
{
  return new HtmlFieldPrefixScope(html.ViewData.TemplateInfo, htmlFieldPrefix);
}

...和...类

...and the class...

private class HtmlFieldPrefixScope : IDisposable
{
    private readonly TemplateInfo templateInfo;
    private readonly string previousHtmlFieldPrefix;

    public HtmlFieldPrefixScope(TemplateInfo templateInfo, string htmlFieldPrefix)
    {
        this.templateInfo = templateInfo;

        previousHtmlFieldPrefix = templateInfo.HtmlFieldPrefix;
        templateInfo.HtmlFieldPrefix = htmlFieldPrefix;
    }

    public void Dispose()
    {
        templateInfo.HtmlFieldPrefix = previousHtmlFieldPrefix;
    }
}

请参阅在下​​面的意见通过钢钣提到的链接。

See the link mentioned by Kohan in the comments below.

这篇关于强制EditorFor至preFIX输入项目上的看法与类名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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