I'm creating multiple forms in a for in my view, the problem comes when I send to the controller on submit and it comes null.
here an example.
@model List<Project.ViewModels.ValidForm>
@if (Model != null)
{
for (int i = 0; i < Model.Count; i++)
{
<div>
@using (Html.BeginForm("Method", "Controller", FormMethod.Post, new { id = "valForm" + @Model[i].Id }))
{
<div class="col-md-4">
<label>Data 1</label><br />
@Html.TextBoxFor(m => m[i].Data1, new { @class = "form-control" })
</div>
<div class="col-md-4">
<label>Data 2 options</label><br />
@Html.TextBoxFor(m => m[i].Data2.Option1, new { @class = "form-control" })
</div>
<div>
<button type="submit" class="btn btn-success">Save this form</button>
</div>
}
</div>
}
}
And there are the viewmodels.
public class ValidForm{
public int data1 { get; set; }
public Data2 data2 {get;set;}
}
public class Data2{
public int option1 {get;set;}
public int option2 {get;set;}
}
and the controller.
[HttpPost]
public ActionResult validaciones(validform vm){
//do something.
return view();
}