Force editor To assign a prefix to input elements in a view with a class name?

I have an editor for:

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

This will bind the transition to a beautiful view (as expected), but will not bind the binding back when the model is sent. This is because the form identifier is not the "Client" prefix.

Usually in this situation, I just pass the model and then bind the inputs to model.Client.PropertyName in the template, but this is not an option in this case, since the template is used on two different viewing models (with the client).

Any suggestions on properly binding this object?

Thanks a lot, Cohan.


Adding

It seems to be a misunderstanding on my part, a problem, as I now understand that fluentHtml does not work inside EditorFor Templates. (The same goes for this fix, which, as it turned out, was not necessary, because EditorFor would prefix for me automatically if I replaced fluentHtml with the usual mvc html helpers)

+5
source share
2 answers

Try something like:

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

Each field that you create using EditorFor, LabelFor and the like will be prefixed.

EDIT: 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 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.

+10
source

HTML MVC3 Name Failure

, 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 would go here...
    </fieldset>
}

, . .

, . :

TryUpdateModel(modelUser.Permission, "Permission");

HTML, TryUpdateModel . .

+5

All Articles