To create an information list you need to list the View data. Example:
Class model:
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 will put only 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 box of the kind and so on with all the input
Controller
[HttpGet()]
public ActionResult Lista()
{
return View();
}
[HttpPost()]
public ActionResult Lista(List<Modelo> model)
{
return RedirectToAction("Lista");
}
When you send the form to ActionResult
with List<Modelo>
the variable model will be loaded with all the 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 remains 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, name of the property Modelo
, indexation of the element [0]
([1]
, [2]
, etc.) and Name of the classe Modelo
, summing up Modelo[0].Tipo
and so on.
Controller
[HttpPost()]
public ActionResult Lista(ViewModelExample example)
{
return RedirectToAction("Lista");
}
Tip, it is an exaggeration often the use of ViewModel
, when there are already classes that can play the same role, everything that is too much in development, can cause problems at the time of maintenance, code repetition, etc.
Maybe this enumeration is best done before this issue by putting in your controller like this:
Controller
[HttpPost()]
public ActionResult Lista(List<Modelo> model, ViewModelExample example)
{
return RedirectToAction("Lista");
}
I didn’t understand different fields!
– user46523
They are the same fields, only replicated several times. In my table I only have 3 columns, I want to replicate in the view and save everything...
– Érik Thiago
but, you need to tell if in View it will have to create the fields or will be 12 lines as default ?
– user46523
The twelve lines as standard
– Érik Thiago
This is not what you want?
– Leonel Sanches da Silva
Maybe so, but I couldn’t understand how it works... @Ciganomorrisonmendez
– Érik Thiago
It seems to me that there are not necessarily 12 fields. It may be more or less. If so, it is the case of Begincollectionitem. So I would still like one more answer?
– Leonel Sanches da Silva
Yes, yes @Ciganomorrisonmendez, no problem!
– Érik Thiago