4

ASP.NET MVC 2 Dots Replaced With Underscore In Element Name (even though ASP.NET MVC will put dots in by default in the name attribute!)

When you stick an element on a form in ASP.NET MVC, it usually does the following:

<%= Html.TextBoxFor(model => model.Contact.FirstName)%>

Becomes

<input type="text" name="Contact.FirstName" id="Contact_FirstName" ...

This is all well and good. However, if you then want to do this:

<%= Html.DropDownList(
    "Contact_Title",
    new SelectList(Model.Titles, "Key", "Value"))%>

You actually end up with

<select id="Contact_Title" name="Contact_Title">...

Note that you now have an underscore not a dot in the name attribute.

So I thought I'd pass in the name, including a dot, like this:

<%= Html.DropDownList(
    "Contact_Title", 
    new SelectList(Model.Titles, "Key", "Value"),
    new { name = "Contact\\.Title" })%>

But it still renders as:

<select id="Contact_Title" name="Contact_Title">...

I really (really) want this to render as:

<select id="Contact_Title" name="Contact.Title">...

In order for this to bind back to Model.Contact.Title automatically - any ideas?

IMPORTANT UPDATE

This is slightly different to my initial thoughts... it actually looks like whatever I pass as the name attribute is simply ignored...

new { name = "MYNAME" }

Still results in Contact_Title!

Any help appreciated!

1 Answer 1

6
<%= Html.DropDownList(
    "Contact.Title",
    new SelectList(Model.Titles, "Key", "Value"))%>

This will render:

<select id="Contact_Title" name="Contact.Title">...

It only replaces the dot in the id as it's not valid in xhtml.

Sign up to request clarification or add additional context in comments.

1 Comment

It will include a dot in the name attribute (that is valid), but not in the id attribute (that is invalid). And it's the same for all helpers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.