Reputation: 3803
I came across a input field in a form
with a method POST
which looks like this
<input name="parm" id="eparm" maxlength="10" type="text">
As far as I know in MVC you can use assign a input field for the variable parm
to a form like this
<input name="parm" id="parm" maxlength="10" type="text">
Can someone explain if it makes a difference to have the id and name field different in MVC view? And on a input
in a form
which attribute indicates the variable reference for the server side code id
or name
?
Upvotes: 0
Views: 1003
Reputation: 14655
name
is what is used to help the model binder figure out how to bind to the data server side. It's especially important when it comes to binding to collections, because you include an index along with that name which allows MVC to distinguish between the different elements in that collection.
As an example, suppose you have the following class:
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
}
Now let's say you want to bind to a collection, like so:
[HttpPost]
public ActionResult UpdateStudents(List<Student> students)
{
// ...
}
In order for this to work, MVC relies upon having the correct name
attribute to allow this to happen. Like so:
<input type="text" name="[0].Id" value="1" />
<input type="text" name="[0].Name" value="Dave" />
<input type="text" name="[1].Id" value="2" />
<input type="text" name="[1].Name" value="Bob" />
The number in each name
attribute is the index. As you can see, this essentially allows the different properties of a model to be associated, based on that index, so now MVC can properly bind the data to List<Student> students
in the action without you having to do anything else to achieve it.
For some other examples of this, including how to let EditorTemplates
generate these names for you, and also how to use non-sequential indices, see Phil Haack's article Model Binding To A List.
Upvotes: 2
Reputation: 151594
I's a bit unclear what you're asking, but the Html.TextBoxFor()
helper for example prints an input element of type="text"
for a model property. When you POST the form to an action method where that model is a parameter, the input element gets bound to the model by the input's name
attribute.
So this model:
public class MyViewModel
{
public string Description { get; set; }
}
Rendered through this view:
@model YourNameSpace.MyViewModel
@using (Html.BeginForm("DoSomething", "YourController", FormMethod.Post))
{
@Html.EditorFor(m => m.Description)
<input type="submit" />
}
Will generate more or less this HTML:
<form method="post" action="/YourController/DoSomething/">
<input type="text" name="Description" />
<input type="submit" />
</form>
Which you can process in this action:
[HttpPost]
public ActionResult DoSomething(MyViewModel model)
{
// Here model.Description will contain what was present in the POST.
return View(model);
}
As for setting the id
, which indeed must be unique on a page, you can use certain overloads of the Html Helpers:
@using (Html.BeginForm("DoSomething", "YourController", FormMethod.Post,
new { id = "someForm" }))
{
@Html.EditorFor(m => m.Description, new { id = "someTextBox" })
<input type="submit" />
}
Upvotes: 0
Reputation: 7900
id
is for DOM manipulation as MUST BE unique. The server know NOTHING about this.
document.getElementById('SOME_ID');
name
is the attribute name that is sent to the server and MAY BE repeated, mainly when you want to send an array of values to the server:
<input type="checkbox" name="choices[]">
<input type="checkbox" name="choices[]">
<input type="checkbox" name="choices[]">
Upvotes: 1