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).
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?
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?
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.
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
This is the macro I'm using
So this is the rendered output of the Html.EditorFor
At least the following attributes should normally be rendered in the input tag
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.
Hi Gert,
I am not entirely sure I understrand the problem, but one thing caughht my eye
I think this:
should be replaced with just this:
As for the validation attributes - Did you enable client side validation and unobtrusive validation in your web.config?
Hope I was helpful
Hi Doron,
the line
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.
Sorry,
My mistake - you are using EditorFor, not TextBoxFor.
Would you like to check if this works?
It works for me.
Did you also enable ClientSideValidation?
Doron
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.
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?
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.
is working on a reply...