0

The following works fine:

@Html.TextBoxFor(x => x.Residence.Rent, new { @class = "form-control"" })
@Html.ValidationMessageFor(x => x.Residence.Rent, null, "span")

But the need is to send a list of objects to the controller. So I have tried this:

@for(int index = 0; index < Model.ResidencesList.Count; index++)
{
    @Html.TextBox("ResidencesList[" + index + "].Rent", ResidencesList[index].Rent, new { id="", @class = "form-control"" })
    @Html.ValidationMessageFor("ResidencesList[" + index + "].Rent", null, "span")
}

which renders the mark up as:

<input class="form-control" name="ResidencesList[0].Rent" type="text" value="">
<span class="field-validation-error" data-valmsg-for="ResidencesList[0].Rent" data-valmsg-replace="true">
   <span>undefined</span>
</span>

The validation doesn't fire on submit and also the message "undefined" shows when I select the textbox.

1 Answer 1

1

try to replace

  @Html.TextBox("ResidencesList[" + index + "].Rent", ResidencesList[index].Rent, new { id="", @class = "form-control"" })
 @Html.ValidationMessageFor("ResidencesList[" + index + "].Rent", null, "span")

with

@Html.TextBoxFor(model=> model.ResidencesList[index].Rent, new {  @class = "form-control"})
 @Html.ValidationMessageFor( model=> model.ResidencesList[index].Rent, "", new { @class = "text-danger" })
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.