Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Gert 81 posts 288 karma points
    Dec 23, 2014 @ 18:02
    Gert
    0

    Why are html attributes not rendered in a partial view used in a Macro

    Hi, I'm using a partial view in combination with a view model and a controller class to create a contact form. The partial view is used in a macro.

    The strange thing is that when using the Html.EditorFor() helper method, the html attributes defined as parameter of the helper method are not rendered, nor are the validation attributes (the viewmodel has validation attribtutes on it's properties).

    Underneath is a part of the partial view

        @inherits Umbraco.Web.Mvc.UmbracoViewPage<Umbraco7.ViewModels.ContactViewModel>
        @using System.Linq
        @using Umbraco7.Code.Umbraco
        @{
            this.MapModel<Umbraco7.ViewModels.ContactViewModel>();
         }
    
        using ( Html.BeginUmbracoForm( "SubmitContactForm", "Communication" ) )
        {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary( true )
    
        ...
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control", autofocus = "autofocus"} })
                @Html.ValidationMessageFor( model => model.Name )
        ...    
    

    This is the macro I'm using

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using Umbraco7.ViewModels;
    @{
        Html.RenderPartial( "~/Views/Partials/contactForm.cshtml", new ContactViewModel() );
    }
    

    So this is the rendered output of the Html.EditorFor

    <input class="text-box single-line" id="Name" name="Name" type="text" value="" />
    

    At least the following attributes should normally be rendered in the input tag

    class="form-control" autofocus="autofocus"
    

    And because I'm using validation attributes on the properties, these should also be rendered but perhaps the this.MapModel removes them? Does anybody have an idea why this is not the case?

    Thank you guys! Regards, Gert.

  • Doron Uziel 23 posts 93 karma points
    Dec 23, 2014 @ 19:10
    Doron Uziel
    1

    Hi Gert,

    I am not entirely sure I understrand the problem, but one thing caughht my eye

    I think this:

    new { htmlAttributes = new { @class = "form-control", autofocus = "autofocus"} }
    

    should be replaced with just this:

    new { @class = "form-control", autofocus = "autofocus"}
    

    As for the validation attributes - Did you enable client side validation and unobtrusive validation in your web.config?

    Hope I was helpful

  • Gert 81 posts 288 karma points
    Dec 23, 2014 @ 19:57
    Gert
    0

    Hi Doron,

    the line

    new { htmlAttributes = new { @class = "form-control", autofocus = "autofocus"} }
    

    Should be correct. I'm using that in other ASP.NET MVC apps with success.

    I forgot indeed the enable the unobtrusive validation but it doesn't make any difference after it has been enabled.

    Does somebody else have any idea?

    Thanks! Gert.

  • Doron Uziel 23 posts 93 karma points
    Dec 23, 2014 @ 20:20
    Doron Uziel
    1

    Sorry,

    My mistake - you are using EditorFor, not TextBoxFor.

    Would you like to check if this works?

     @Html.TextBoxFor(model => model.Name,  new { @class = "form-control", autofocus = "autofocus"} )
    

    It works for me.

    Did you also enable ClientSideValidation?

    Doron

  • Gert 81 posts 288 karma points
    Dec 26, 2014 @ 16:45
    Gert
    0

    Hi Doron,

    @Html.TextBoxFor() works for the html attributes, but the validation attributes are still not showing up while clientside validation is enabled.

    Still don't understand why @Html.EditorFor() is nog working as aspected?

    Regards, Gert.

  • Doron Uziel 23 posts 93 karma points
    Dec 27, 2014 @ 16:50
    Doron Uziel
    0

    To be honest I hardly ever use EditorFor so I have no clue.

    Did you remember to iclude jquery.validate.js and jquery.validate.unobtrusive.js libraries in your page? Does your input render with data-val-* attributes?

  • Gert 81 posts 288 karma points
    Dec 29, 2014 @ 10:13
    Gert
    0

    Hi Doron,

    Yes, jquery.validate.js is included. I found out why the data-val-* attributes are not rendered, because I'm using custom validation attributes (derived from the .net ones) for localization. I have to investigate it further to find out why a derived attribute is not working.

    Still don't know why the EditorFor helper method is not working though.

    Regards, Gert.

Please Sign in or register to post replies

Write your reply to:

Draft