How can I add elements to a list of objects and display it in a view without replacing the previous one through a form?

0

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>   
  }

  

    
asked by Jackson Cuevas 28.07.2018 в 11:05
source

1 answer

0

There are three options that I can think of. The first that I would recommend is to do it from the client side, in this case the view you have with a javascritp library, such as AngularJS or jQuery, so you avoid the call to the server.

I can see how to do it with angle using ng-repeat here link

The second, which I do not recommend at all, is that your "Person" list is static, that will prevent the content from being deleted. But you should document a bit about the use of static variables.

For this, you only add in your controller:

static List<Person> personasList = new List<Person> () { };

A third way is to use the TempData to keep the information in the current list.

In the controller

personasList = TempData["miLista"];

And in the view, the inverse

TempData["miLista"] = personasList;

I hope this can help you.

    
answered by 28.07.2018 / 18:11
source