Git Product home page Git Product logo

Comments (2)

johnnyreilly avatar johnnyreilly commented on June 29, 2024

Hi @davidcarroll

Thanks both for your kind words and for bringing this to my attention.

I'm not generally a user of EditorTemplates but I do want other people to be able to use them if that's their preferred approach. I was as equally baffled as you as to why this is happening. So much so that I ended up digging through the MVC source to try and understand it a little better. Unfortunately this didn't really reveal why it was working this way; rather just confirming that indeed it did work this way. Not terribly helpful...

My aim for the project is for the jQuery.Validation.Unobtrusive.Native helpers to mirror as closely as possible the out-of-the-box MVC helpers. Your example is evidence of us moving away from that and in the absence of a clearly better approach (I haven't come up with one) I'm keen to take some steps to move us back in line. To that end I've taken your approach and moved it into Mapper.GenerateHtmlWithoutMvcUnobtrusiveAttributes as below:

public static IHtmlString GenerateHtmlWithoutMvcUnobtrusiveAttributes(
    ModelMetadata metadata, TemplateInfo templateInfo,
    Func<string, IHtmlString> htmlGenerator)
{
    var cachedClientValidationEnabled = HtmlHelper.ClientValidationEnabled;
    var cachedUnobtrusiveJavaScriptEnabled = HtmlHelper.UnobtrusiveJavaScriptEnabled;

    HtmlHelper.ClientValidationEnabled = false;
    HtmlHelper.UnobtrusiveJavaScriptEnabled = false;

    // Clear the name if the HtmlFieldPrefix already contains it (typically only in EditorTemplates)
    var name = metadata.PropertyName;
    if (templateInfo.HtmlFieldPrefix.EndsWith(metadata.PropertyName))
        name = name.Substring(0, name.LastIndexOf(metadata.PropertyName, StringComparison.InvariantCulture));

    var html = htmlGenerator(name);

    HtmlHelper.ClientValidationEnabled = cachedClientValidationEnabled;
    HtmlHelper.UnobtrusiveJavaScriptEnabled = cachedUnobtrusiveJavaScriptEnabled;

    return html;
}

Then in each of the Extensions files you can see the call into Mapper.GenerateHtmlWithoutMvcUnobtrusiveAttributes has subtly changed:

TextBoxFor

// ....
var textBox = Mapper.GenerateHtmlWithoutMvcUnobtrusiveAttributes(metadata, htmlHelper.ViewContext.ViewData.TemplateInfo,
    (propName) => htmlHelper.TextBox(propName, metadata.Model, format, attributes));
// ....

TextAreaFor

// ....
var textArea = Mapper.GenerateHtmlWithoutMvcUnobtrusiveAttributes(metadata, htmlHelper.ViewContext.ViewData.TemplateInfo,
    (propName) => htmlHelper.TextArea(propName, metadata.Model as string, rows, columns, attributes));
// ....

RadioButtonFor

// ....
var radioButton = Mapper.GenerateHtmlWithoutMvcUnobtrusiveAttributes(metadata, htmlHelper.ViewContext.ViewData.TemplateInfo,
    (propName) => htmlHelper.RadioButton(propName, value, attributes));
// ....

ListBoxFor

// ....
var listBox = Mapper.GenerateHtmlWithoutMvcUnobtrusiveAttributes(metadata, htmlHelper.ViewContext.ViewData.TemplateInfo,
    (propName) => htmlHelper.ListBox(propName, selectList, attributes));
// ....

DropDownListFor

// ....
var dropDown = Mapper.GenerateHtmlWithoutMvcUnobtrusiveAttributes(metadata, htmlHelper.ViewContext.ViewData.TemplateInfo,
    (propName) => htmlHelper.DropDownList(propName, selectList, optionLabel, attributes));
// ....

CheckBoxFor

// ....
var checkBox = Mapper.GenerateHtmlWithoutMvcUnobtrusiveAttributes(metadata, htmlHelper.ViewContext.ViewData.TemplateInfo,
    (propName) => htmlHelper.CheckBox(propName, value, attributes));
// ....

With this approach in place all existing unit tests (alas still not a full suite) pass and initial hands-on tests look good.

If you wanted to fork and issue a pull request with the above changes in I'd happily take it. Alternately I'm happy to check my own changes in with this in place. Just wanted to give you the opportunity to have credit it for this.

from jquery.validation.unobtrusive.native.

johnnyreilly avatar johnnyreilly commented on June 29, 2024

Fixed with #2

from jquery.validation.unobtrusive.native.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.