Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否强制EditorFor在视图中的输入项前面加上类名?_C#_Asp.net Mvc_Binding_Asp.net Mvc 3_Editorfor - Fatal编程技术网

C# 是否强制EditorFor在视图中的输入项前面加上类名?

C# 是否强制EditorFor在视图中的输入项前面加上类名?,c#,asp.net-mvc,binding,asp.net-mvc-3,editorfor,C#,Asp.net Mvc,Binding,Asp.net Mvc 3,Editorfor,我有一个编辑: <%: Html.EditorFor(model => model.Client, "ClientTemplate", new { editing = false })%> model.Client,“ClientTemplate”,新建{editing=false})%> 这将绑定到视图(如预期的那样),但不会在发布模型时绑定回视图。 这是因为表单id的前缀不是“Client” 通常在这种情况下,我只需传入模型,然后将输入绑定到模板中的model.Clie

我有一个编辑:

<%: Html.EditorFor(model => model.Client, "ClientTemplate", new { editing = false })%>
model.Client,“ClientTemplate”,新建{editing=false})%>
这将绑定到视图(如预期的那样),但不会在发布模型时绑定回视图。 这是因为表单id的前缀不是“Client”

通常在这种情况下,我只需传入模型,然后将输入绑定到模板中的model.Client.PropertyName,但在这种情况下,这不是一个选项,因为模板用于两个不同的viewmodels(打开客户端)

有什么建议可以让它正确绑定吗

非常感谢,, 科汉


补遗 这似乎是我的误解,我现在理解的问题是fluentHtml在EditorFor模板中不起作用。(此修复程序也是如此,如果我使用普通mvc html帮助程序替换fluentHtml,则EditorFor将自动为我添加前缀)

尝试以下操作:

<% Html.BeginHtmlFieldPrefixScope("Client") {
  Html.EditorFor(model => model.Client, "ClientTemplate", new { editing = false });
<% } %>
…还有这个班

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;
    }
}
请参阅Kohan在下面评论中提到的链接。

MVC3 HTML名称冲突 在MVC3中不起作用的剪切和粘贴。为了让扩展正常工作,我必须创建一个类文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

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

        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;
            }
        }
    }
}
在Razor(.cshtml)文件中,我添加了以下内容:

@using incMvcSite.Classes
@using(Html.BeginHtmlFieldPrefixScope("Permission")) {
    <fieldset>
        <legend>Permission</legend>

        // The Html.EditorFor's would go here...
    </fieldset>
}

这会将前缀添加到HTML中的所有字段中,TryUpdateModel会加载带有前缀控件名称的对象。现在,您可以为嵌入的编辑列表和具有相同属性名称的模型的局部视图正确地命名控件的名称空间。

您是指本问题中提到的自定义扩展方法吗@科汉:没错,我不知道密码是从哪里来的!谢谢您的示例中还有一个错误,应该是:Html.EditorFor(model=>model.Client,“ClientTemplate”,new{editing=false});但无论如何,非常感谢,我希望现在能到那里。对不起,看来这一切都是徒劳的。EditorFor会自动执行此操作,但正是我模板中的fluentHtml将其搞乱了。
@using incMvcSite.Classes
@using(Html.BeginHtmlFieldPrefixScope("Permission")) {
    <fieldset>
        <legend>Permission</legend>

        // The Html.EditorFor's would go here...
    </fieldset>
}
TryUpdateModel(modelUser.Permission, "Permission");