Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

control name in TextBoxFor in MVC3

Is it possible to control the name attribute of say a text boxt when using TextBoxFor?

this code in the view

@Html.TextBoxFor(m => m.SearchParams.someParam)

produce this

<input id="SearchParams_someParam" name="SearchParams.someParam" type="text" value="">

but I do not want my input name to be "SearchParams.someParam" and I wanted that to be something like

<input id="SearchParams_someParam" name="MyPreferedName" type="text" value="">

where the MyPreferedName comes from from some attributes the .SearchParams.someParam in the corresponding model.

Is this possible? I know @Html.TextBox does it but I do not want to hardcode the name in the view.

like image 492
iCode Avatar asked Aug 03 '11 02:08

iCode


People also ask

What is the difference between TextBoxFor and Editorfor?

it's absolutly wrong answer, becuase the key difference is that Texbox returns input and editorfor returns your template where input is default template for editorfor.

What is difference between TextBox and TextBoxFor?

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.

How disable HTML TextBoxFor in MVC?

Disabled inputs don't POST back to the server. You can make the input 'read only' by using 'readonly'. This will let the data be POSTED back, but the user cannot edit the information in the traditional fashion. Keep in mind that people can use a tool like Firebug or Dragonfly to edit the data and post it back.

What is TextBoxFor in MVC?

TextBoxFor represents a single-line input control that allows end-users to enter text.


1 Answers

@Html.TextBoxFor(model => model.attr, new { Name = "txt1" })

Just Use "Name" instead of "name"

like image 60
u_1958148 Avatar answered Oct 10 '22 03:10

u_1958148