To create a list of information you need to enumerate the View data. Example:
Class Template:
using System;
namespace WebApplication10.Models
{
public class Modelo
{
public string Tipo { get; set; }
public DateTime DataHora { get; set; }
public int AlimentosQuantidades { get; set; }
}
}
Fixed view with 12 rows and 3 columns
@using (Html.BeginForm())
{
for (int i = 0; i < 12; i++)
{
<div class="row">
<div class="col-md-4">
@Html.TextBox(string.Format("[{0}].Tipo", i), null, new { @class = "form-control" })
</div>
<div class="col-md-4">
@Html.TextBox(string.Format("[{0}].DataHora", i), null, new { @class = "form-control" })
</div>
<div class="col-md-4">
@Html.TextBox(string.Format("[{0}].AlimentosQuantidades", i), null, new { @class = "form-control" })
</div>
</div>
}
<button>Enviar</button>
}
The code generated by this View (I'll only put 2 lines)
<div class="col-md-4">
<input class="form-control" name="[0].Tipo" type="text" value="" />
</div>
<div class="col-md-4">
<input class="form-control" name="[0].DataHora" type="text" value="" />
</div>
<div class="col-md-4">
<input class="form-control" name="[0].AlimentosQuantidades" type="text" value="" />
</div>
</div>
<div class="row">
<div class="col-md-4">
<input class="form-control" name="[1].Tipo" type="text" value="" />
</div>
<div class="col-md-4">
<input class="form-control" name="[1].DataHora" type="text" value="" />
</div>
<div class="col-md-4">
<input class="form-control" name="[1].AlimentosQuantidades" type="text" value="" />
</div>
</div>
So with [0].Tipo
is the first box and [1].Tipo
is the next type box and so on with all input
Controller
[HttpGet()]
public ActionResult Lista()
{
return View();
}
[HttpPost()]
public ActionResult Lista(List<Modelo> model)
{
return RedirectToAction("Lista");
}
When you send the form in ActionResult
with List<Modelo>
the variable model will be loaded with all information
EDIT
When you have a ViewModel
and a property that represents this field listing, in your View
put the name of the property and the rest remain the same.
Example:
ViewModel
using System;
namespace WebApplication10.Models
{
public class ViewModel
{
public List<Modelo> Modelo {get;set;}
//... os outros itens.
}
}
View
@using (Html.BeginForm())
{
for (int i = 0; i < 12; i++)
{
<div class="row">
<div class="col-md-4">
@Html.TextBox(string.Format("Modelo[{0}].Tipo", i), null, new { @class = "form-control" })
</div>
<div class="col-md-4">
@Html.TextBox(string.Format("Modelo[{0}].DataHora", i), null, new { @class = "form-control" })
</div>
<div class="col-md-4">
@Html.TextBox(string.Format("Modelo[{0}].AlimentosQuantidades", i), null, new { @class = "form-control" })
</div>
</div>
}
<button>Enviar</button>
}
That is, property name Modelo
, indexing element [0]
( [1]
, [2]
, etc.) and Field name of classe Modelo
, summarizing Modelo[0].Tipo
and so on. p>
Controller
[HttpPost()]
public ActionResult Lista(ViewModelExample example)
{
return RedirectToAction("Lista");
}
Tip, it is often an exaggeration to use ViewModel
, when you already have classes that can do the same role, anything that is too excessive in development, can cause problems at the time of maintenance, code repetition, etc. .
Perhaps this enumeration is best done before this issue by putting in your controler like this:
Controller
[HttpPost()]
public ActionResult Lista(List<Modelo> model, ViewModelExample example)
{
return RedirectToAction("Lista");
}