Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set id to Html.TextBox item on MVC

Im trying to catch the textbox element by javascript, in order to put text into it. so how to set id to the textbox ??

        <tr>                     // Id to this textbox             <td>@Html.TextBoxFor(item => item.OperationNo)</td>         </tr> 

and then to put text into it by JS

                            //     document.getElementById("Textbox id").Text= " Some text " ; 
like image 594
Fadi Alkadi Avatar asked Jun 06 '13 18:06

Fadi Alkadi


People also ask

How can we give ID to TextBox in HTML?

By default, the TextBoxFor element will have an ID and NAME property that matches the expression property of the element. If you want to specify an ID or NAME that's different from the expression property, you can use the htmlAttributes overload param. Thanks!

How do you give ID in Razor syntax?

Just add the id property to the html-attributes. That will override the default id generated by the editorfor-helper-methode.

How do I set the MaxLength for HTML TextBoxFor in MVC?

The TextBox for the Name value is created using Html. TextBoxFor function while the TextBox for the Mobile Number value is created using Html. TextBox helper function. The MaxLength of both the TextBoxes is set using the HTML MaxLength attribute using the HtmlAttributes parameter in Html.

What is the difference between HTML TextBox vs HTML TextBoxFor in MVC?

IMO the main difference is that Textbox is not strongly typed. TextboxFor take a lambda as a parameter that tell the helper the with element of the model to use in a typed view. You can do the same things with both, but you should use typed views and TextboxFor when possible.


1 Answers

You can set ID of a textbox like this.

@Html.TextBoxFor(item => item.OperationNo, new { id = "MyId" }) 

OR

@Html.TextBoxFor(item => item.OperationNo, htmlAttributes: new { id = "MyId" }) 

Output:

<input ... id="MyId" name="OperationNo" type="text" /> 
like image 177
Win Avatar answered Oct 05 '22 22:10

Win