0

Is it possible to give my HtmlHelper textbox an id ? For instance like

<input type="submit" id="someid" />

In this case it would be for Html.Textbox

The reason I am asking this is because I want to use this info in a javascript function and get the value like this

(document.getElementById('someid').value)

Suggestions anyone ?

Shinji
  • 85
  • 1
  • 11

4 Answers4

2

If using the Html.TextboxFor() method, @Html.TextboxFor(model=>model.Property);, your textbox will have an id with the name of the property. My example above would create an HTML tag of

<input type="text" id="Property" name="Property"/>

Tommy
  • 39,592
  • 10
  • 90
  • 121
1

You can specify some metadata when creating the link on the server.

[C#]

    <%= Html.ActionLink(“Edit Record”, “Edit”, new {Id=3})

[VB]

    <%= Html.ActionLink(“Edit Record”, “Edit”, New With {.Id=3})%>

Taken from: http://stephenwalther.com/blog/archive/2009/03/03/chapter-6-understanding-html-helpers.aspx

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
1

As Tommy said, The element will be created with the same name as of the Property Id. But you can override it by using this Overload

 @Html.TextBoxFor(m => m.FirstName, new {@id="myNewId" })

This will create an input element with an Id "myNewId" . But the name stays same as the property Name.

Shyju
  • 214,206
  • 104
  • 411
  • 497
0

Instead of giving the textbox an id, you can ask ASP.NET MVC to tell you what it is, as per the answers in this question.

Community
  • 1
  • 1
robaker
  • 1,028
  • 7
  • 11