How to return a list to a controller by post

0

I load my list by getting a list of people and the charge in an html table but then I have to return the same list by post but it returns empty to me

 [HttpGet]
    public ActionResult GestionUsuarios(string f)
    {
        Response res = new Response();
        DataMorce<Usuario> bdUsuario = new DataMorce<Usuario>();
        DataMorce<Archivo> bdArchivos = new DataMorce<Archivo>();
        GestionUsuariosViewModel gestionView = new GestionUsuariosViewModel();

        if (HttpContext.Session["Idusers"] == null || HttpContext.Session["users"] == null)
        {
            LevantarSesiones(System.Web.HttpContext.Current.User.Identity.Name);
        }
        int idInstitucion = (int)Session["IdInstitucion"];
        int idUsuario = Convert.ToInt32(Session["users"]);

        List<Persona> listaPer = bdInstitucion.GetFilter(x => x.IdInstitucion == idInstitucion).SelectMany(p => p.Personas).OrderBy(x => x.Nombres).ToList();

        var listaPersonas = new List<Personas>();
        listaPersonas = listaPer;


        gestionView.ListaPersonas = listaPersonas;
        return View(gestionView);
    }

and then by post it returns the same empty list

 [HttpPost]
    public ActionResult GestionUsuarios(List<Persona> listaPersonas, GestionUsuariosViewModel gestionView)
    {
        GestionUsuariosViewModel gestionUserView = new GestionUsuariosViewModel();
        List<Persona> listaPers= new List<Persona>();
        Response res = new Response();
        DataMorce<Usuario> bdUsuario = new DataMorce<Usuario>();

        int idInstitucion = (int)Session["idinstitucion"];
        int idUsuario = Convert.ToInt32(Session["users"]);

        var l = ViewBag.Model;
        //
        return View(listaPersonas);
    }

The html view

@model WEBMORCE.Model.GestionUsuariosView.GestionUsuariosViewModel @{ ViewBag.Title = "Gestión de Usuario"; }
<!DOCTYPE html>

<html>

<head>
  <meta name="viewport" content="width=device-width" />
  <title>GestionUsuarios</title>

</head>

<body>
  <div class="row col-md-12">
 @using (Html.BeginForm("GestionUsuarios", "GestionUsuarios", FormMethod.Post, new { enctype = "multipart/form-data" }))
                                    {
                                        <input type="submit" value="Generar usuarios" class="btn btn-success pull-right" />
                                    }
    <table class="footable table table-stripped" data-page-size="8" data-filter=#filter>
      <thead>
        <tr>
          <th class="col-md-1"><input type="checkbox" id="checkTodos" class="i-checks" name="input[]"></th>
          <th>Nombre </th>
          <th>Usuario </th>
          <th>Opciones</th>
        </tr>
      </thead>
      <tbody>
        @foreach (var item in Model.ListaPersonas) {
        <tr class="gradeA">
          <td class="col-md-1">
            @Html.CheckBoxFor(model => item.isChecked, new { idPersona = item.IdPersona, @class = "i-checks" })
          </td>
          <td>
            <div class="pull-left">
              <div class="text-center">
                @if (!string.IsNullOrEmpty(item.UrlFoto)) {
                <img src="@Url.Content(item.UrlFoto)" alt="Perfil" class="zoom img-circle" style="height:30px; width:30px" /> }
              </div>
            </div>
            &nbsp;&nbsp; @Html.DisplayFor(modelItem => item.NombreCompleto)
          </td>
          <td>
            @if (item.NombreUser == "Sin Usuario") {
            <span class="badge text-bold bg-danger" title="Sin usuario">&nbsp;&nbsp;</span> } else {
            <span class="badge text-bold bg-primary" title="Usuario activo">&nbsp;&nbsp;</span> }
            <span class="pie">
                                                                    @item.NombreUser
                                                                </span>
          </td>
          <td>
            <div class="btn-group">
              <button data-toggle="dropdown" class="btn btn-primary btn-sm dropdown-toggle">Opciones <span class="caret"></span></button>
              <ul class="dropdown-menu">
                <li><a href='@Url.Action("InformacionParticipante", "Estudiantes", new { idPersona = item.IdPersona })'>Editar Información</a></li>
                <li><a href="#">Actividad/Auditoria *</a></li>
                <li><a href="#">Restablecer clave *</a></li>
                <li class="divider"></li>
                <li><a href="#">Recordar clave *</a></li>
              </ul>
            </div>
          </td>
        </tr>
        } @*@Html.ActionLink("Listados", "GestionUsuarios", new { listaPersona = Model.ListaPersonas})*@
      </tbody>
      <tfoot>
        <tr>
          <td colspan="5">
            <ul class="pagination pull-right"></ul>
          </td>
        </tr>
      </tfoot>
    </table>

</body>

</html>

Thanks

    
asked by Juan C. Martinez 03.04.2018 в 19:08
source

2 answers

0

The solution I had was to change the razor controls I had for native html controls

<div class="panel-body">
  @*@Html.Action("GridView3Partial", "GestionUsuarios")*@

  <form method="post" action="/GestionUsuarios/GestionUsuarios">
    <table class="table table-bordered" id="datos" data-page-size="8" data-filter=#filter>
      <thead>
        <tr>
          <th>Nombre </th>
          <th>Usuario </th>
          <th>Opciones</th>
        </tr>
      </thead>
      <tbody>
        @foreach (var item in Model.ListaPersonas) {
        <tr>
          <td>
            <div class="pull-left">
              <div class="text-center">
                @if (!string.IsNullOrEmpty(item.UrlFoto)) {
                <img src="@Url.Content(item.UrlFoto)" alt="Perfil" class="zoom img-circle" style="height:30px; width:30px" /> }
              </div>
            </div>
            &nbsp;&nbsp; @Html.DisplayFor(modelItem => item.NombreCompleto)
          </td>
          <td>
            @if (item.NombreUser == "Sin Usuario") {
            <span class="badge text-bold bg-danger" title="Sin usuario">&nbsp;&nbsp;</span> } else {
            <span class="badge text-bold bg-primary" title="Usuario activo">&nbsp;&nbsp;</span> }
            <span class="pie">
                                                                    @item.NombreUser
                                                                </span>
          </td>
          <td>
            <div class="btn-group">
              <button data-toggle="dropdown" class="btn btn-primary btn-sm dropdown-toggle">Opciones <span class="caret"></span></button>
              <ul class="dropdown-menu">
                @*
                <li><a href='@Url.Action("InformacionParticipante", "Estudiantes", new { idPersona = item.IdPersona })'>Editar Información</a></li>*@
                <li><a href="#">Editar Información</a></li>
                <li><a href="#">Restablecer clave</a></li>
                <li><a href="#">Recordar clave</a></li>
                <li class="divider"></li>
                <li><a href="#">Asignar Institución</a></li>
              </ul>
            </div>
          </td>
        </tr>
        }
      </tbody>
      <tfoot>
        <tr>
          <td colspan="5">
            <ul class="pagination pull-right"></ul>
          </td>
        </tr>
      </tfoot>
    </table>
  </form>

</div>
    
answered by 06.04.2018 / 17:44
source
0

Could you put the HTML code that is generated?

When I had to do something similar, it worked for me:

[HttpPost]
public ActionResult GestionUsuarios(List<string> listaPersonas, GestionUsuariosViewModel gestionView)
    {
    }

The HTML that generates your view must be of the following style:

<input type="checkbox" name="listaPersonas" value="loquesea">

The value of the checkbox can be read as a string.

I hope it serves you.

Greetings

    
answered by 04.04.2018 в 10:48