Models/Person.cs
public class PersonController : Controller {
List<Person> personasList = new List<Person> () { };
public IActionResult Index () {
return View ();
}
[HttpPost]
public IActionResult Index (Person persona) {
personasList.Add (persona);
return View(personasList);
}
}
Controllers/PersonController.cs
public class PersonController : Controller {
List<Person> personasList = new List<Person> () { };
public IActionResult Index () {
return View ();
}
[HttpPost]
public IActionResult Index (Person persona) {
personasList.Add (persona);
return View(personasList);
}
}
Views/Person/Index.cshtml
@model IEnumerable<MyMVC.Models.Person>
@{
ViewData["Title"] = "Index";
}
<h2>Index</h2>
<div class="row">
<div class="col-md-4">
<form method="post">
<div class="form-group">
@Html.Label("Nombre:")
@Html.TextBox("firstName", null, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.Label("Apellido:")
@Html.TextBox("lastName", null, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.Label("Edad:")
@Html.TextBox("Age", null, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.Label("Email:")
@Html.TextBox("Email", null, new { @class = "form-control" })
</div>
<hr/>
<button type="submit">Enviar</button>
</form>
</div>
</div>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.firstName)
</th>
<th>
@Html.DisplayNameFor(model => model.lastName)
</th>
<th>
@Html.DisplayNameFor(model => model.Age)
</th>
<th>
@Html.DisplayNameFor(model => model.Email)
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.firstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.lastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Age)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
</tr>
}