Send a grid entity by POST

2

I have a view created with Razor with the list template based on a model, the view is fine and it shows in a grid the data of the list, and also has its 3 links (edit datail delete), what I need is that one of those links (or add a fourth link or button) allows me to capture the value of the record (a row of the grid that basically is a tr of the table) whole (of an instance of the model) and pass it to an action of the controller by means of post. There are N examples of how to retrieve a data from the row of the grid and pass it through the URL to an action of a controller, I do not want to pass data in the url, and not only do I need a data, I need the entire row.

Or in the last case, how can I pass the value of a cell in the grid by means of a post and how do I recover it? This is not exactly what I need, but to begin with, it can serve me very well.

Note: As an analogy, I give the example that in webform (I already know that mvc and webform are totally different) you can select a record in the grid and in the select event access each of them the attributes of the grid as it was a matrix, without having to pass it through the url an attribute or anything like that. Something similar I want to be able to do from MVC

    
asked by RSillerico 25.11.2016 в 01:35
source

2 answers

1

I would do it in the following way:

  • In the Scripts section of your view write a " class "that contains all the fields that you use and taking advantage of Razor, creates an arrangement and fill it once:

    @section Scripts
    {
        <script>
            function model(id, valor1, valor2...){
                this.id = id;
                this.valor1 = valor1;
                this.valor2 = valor2;
                ...
            }
    
            var miArray = [];
            @foreach(var x in Model)
            {
                <text>miArray.push(new model("@x.Id", "@x.Valor1", "@x.Valor2"...);</text>
            }
        </script>
    }
    
  • Also in the script, create a function that receives as a parameter some identifier of the line and make the call Ajax from there:

    function sendRow(id){
        //Encuentra un "x" tal que el id de ese "x" sea igual al id del parámetro.
        let renglonPorEnviar = miArray.find(x => x.id == id);
        $.ajax({
            url: "tu/url",
            data: renglonPorEnviar,
            type: "POST",
            dataType: "Json",  //Si aplica
            success: function(data){ ... }
        });
    }
    

    (Here I use the function find of JS to select the element).

  • (I also define the variable as let instead of var for a local scope only).

  • Add a button to your row in the table that has the function of step 2 linked to it:

    <button onclick="sendRow('@Model...ID')">Enviar</button>
    
  • answered by 06.09.2017 в 16:10
    0

    I give you an example that I used a few years ago. It was a grid with many rows. In each one it had 2 options and with actionlink it sent several values to the controller.

    BES was an object within a collection (the grid was a collection of BES objects) and sent the attributes it needs from the object in the chosen row.

    I hope that the example will help you to get an idea of how to do.

    The view:

    <td>
     @Html.ActionLink("Ver/Editar", "VerEditar", "BombaElectroSumergible", new { IDBombaElectroSumergible = BES.IDBombaElectroSumergible, IDPozo = Model.IDPozo, FechaFinFuncionamiento = BES.FechaFinFuncionamiento, FechaInicioFuncionamiento = BES.FechaInicioFuncionamiento }, null)
                    @if (@Roles.IsUserInRole(@HttpContext.Current.User.Identity.Name, "Administrador"))
                    {
                        @Html.ActionLink("| Borrar", "Delete", "BombaElectroSumergible", new { IDBombaElectroSumergible = BES.IDBombaElectroSumergible, IDPozo = Model.IDPozo }, new { onclick = "return confirm('Borrar BES?');" })} </td>
    

    The driver:

    public ActionResult VerEditar(int IDBombaElectroSumergible, int IDPozo, DateTime FechaFinFuncionamiento, DateTime FechaInicioFuncionamiento)
        {
    
        
    answered by 25.11.2016 в 01:55