0

I get a duplicate name attribute while validating through https://validator.w3.org

@Html.EditorFor(model => model.User_Name, new { htmlAttributes = new { @class = "form-control", id = "txtFullNameRealtor", @Name = "txtFullNameRealtor", placeholder = "Full Name" } })

I get this in the html source

<input Name="txtFullNameRealtor" class="form-control text-box single-line" id="txtFullNameRealtor" name="User_Name" placeholder="Full Name" type="text" value="" />

As you can see there,two name attributes are generated that is Name="txtFullNameRealtor" and name="User_Name".

Is there a way to make it generate a single name attribute ?

And I want the Name attribute be explicitly set by me to be there.

Ayush
  • 11
  • 2
  • 1
    remove ` @Name = "txtFullNameRealtor"`? – Liora Haydont Apr 19 '18 at 14:12
  • you added a second name attribute in your attribute declarations. – Fran Apr 19 '18 at 14:16
  • I want @Name = "txtFullNameRealtor"` to be the Name Attribute and not the one that is generated by default . – Ayush Apr 19 '18 at 14:18
  • Under no circumstances do you ever set the `name` attribute when using `HtmlHelper` methods - the method generated the correct html, including the name attribute for model binding and validation. –  Apr 19 '18 at 22:32
  • And if you want a different name attribute, the just create the input manually (there is no point using a `HtmlHelper` method when your do not want model binding or validation) –  Apr 19 '18 at 22:35
  • @StephenMuecke It is my compulsion, can you help me with what I need? – Ayush Apr 20 '18 at 08:55
  • Why do you want to change the `name` attribute? - it make no sense at all and will mean you cannot bind to your model, and cannot get client side validation –  Apr 20 '18 at 09:09
  • @StephenMuecke I do not want to lose the model binding and the Model Validations. Is there no way I can have both with my requirements fulfilled. Thank You already for sparing your time to address my concern. – Ayush Apr 20 '18 at 09:38
  • Then do not change the `name` attribute (its only purpose to so generate the name/value pairs that are sent in the request when submitting your form) - Again, why do you want to change it and ensure your app does not work correctly –  Apr 20 '18 at 09:40
  • @StephenMuecke There is already some old code written by someone else who has used the second name attribute to get the values on server. The Code is used everywhere and i did not want to disturb it. I thought a simple change could solve my problem and save me a lot of time – Ayush Apr 20 '18 at 09:44

3 Answers3

0

You can try this

@Html.EditorFor creates name same as that of property name.

if you want to specific name you can use @Html.Editor instead of @html.EditorFor. i hope this helps

@Html.Editor("txtFullNameRealtor" er_Name, new { htmlAttributes = new { @class = "form-control", id = "txtFullNameRealtor",  placeholder = "Full Name" } }
user9405863
  • 1,506
  • 1
  • 11
  • 16
0

You could acheive this with the following:

@Html.TextBox("txtFullNameRealtor", Model.User_Name, new { @class = "a-class-name" })

As it looks like you want a text box anyway you can use the text box helper. The first argument is where the name/id are derived from, the second argument is the value and the 3rd are the htmlAttribtues. There are more overloads as well.

John
  • 685
  • 1
  • 6
  • 20
0

User Html.Editor() instead of Html.EditorFor().
EditorFor will infer the html element name from the model property that is "bound" to it.

Editor allows you to declare whatever html name you would like with the last parameter.

public static MvcHtmlString Editor(
    this HtmlHelper html,
    string expression,
    string templateName,
    string htmlFieldName

It sounds like you are binding a domain model directly to your UI. I'd suggest create a viewmodel with the property names you want and translating between your view model and domain model in your controller instead.

Fran
  • 6,440
  • 1
  • 23
  • 35