I am trying to generate a unique label and and input text box for a partial view that is being used to render a list of user input rows.
By unique I mean that each input text box should have its unique html "id" and "name" so that when is submitted each input can be identified
In the View I have
@model UserDataModel
@{
var inpName = "benefName" + @Model.Row;
var inpAge = "benefAge" + @Model.Row;
}
@Html.LabelFor(x => x.Name, new { @class="labelhalf"})
@Html.EditorFor(x => x.Name, new { id = @inpName, htmlAttributes = new { @class = "form-control animated" } })
When the view is being render this is what I am seeing
<label class="labelhalf" for="Name">Nombre (Opcional)</label>
<input class="text-box single-line" id="Name" name="Name" type="text" value="">
As you can see the "name" and "id" attributes of the text input is "Name" and "Name" and is not using the value of the @inpName variable ("benefName1" for example)
Also I am trying to assign some CSS classes to that same input using "htmlAttributes"
I had previously tried this with this approach
<label form="FormStep_01" for=@inpName class="labelhalf">Nombre (Opcional)</label>
<input form="FormStep_01" id=@inpName class="form-control animated" pattern="^[_A-z0-9]{1,}$" type="text" placeholder="" required="">
...but the content of the input fields with this approach are not being submited and that is the reason I am trying to use the @Html.EditorFor
UPDATE I am now using the TextBoxFor which takes the "id" and the "class" fine but not the "name" which is used in the submit
@Html.LabelFor(x => x.Name, new { @class = "labelhalf" })
@Html.TextBoxFor(x => x.Name, new { @id = @inpName, name = @inpName, @class = "form-control animated" })
Please let me know how to achieve this in MVC4