Shinji
Shinji

Reputation: 85

Is it possible to give my HtmlHelper textbox an id ?

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 ?

Upvotes: 0

Views: 1846

Answers (4)

robaker
robaker

Reputation: 1029

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.

Upvotes: 0

Shyju
Shyju

Reputation: 218732

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.

Upvotes: 1

Kevin Bowersox
Kevin Bowersox

Reputation: 94459

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

Upvotes: 1

Tommy
Tommy

Reputation: 39807

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"/>

Upvotes: 2

Related Questions