ASP.Net MVC - Send data from a view to a controller

1

How do I do the following?

I have a classic view with tables created with razor as follows:

...
@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.a)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.b)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.c)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.d)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.e)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.f)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.g)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.h)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.i)
        </td>
        <td>
            @Html.ActionLink("Listados", "listar", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
        </td>
    </tr>
}
....

Clearly it is a grid, that for each record has 3 links, the first one that has the name of Listing goes to the action to list, now, the question of the million is:

  

When I click on List, how do I pass the selected entity to the list action? and also how do I pass the value of one of the columns to the action list? obviously I also need to know how I recover the value in the stock. Preferably that this data does not pass through the URL.

Controller action:

public ActionResult listar(string dato)
{
    CTPP oTar  = new CTPP ();
    oTar.strCodEmpresa = "1";
    oTar.strNroTarjeta = dato;
    oTar.datFechaIni = DateTime.Parse("01/01/2016");
    oTar.datFechaFin = DateTime.Parse("12/12/2016");

    CLIS oLis = Servicioweb.listar(oTar);

    return View(oLis.lista);
}

Or else something more key would be, in the same scenario of the grid and the links, how did a data of the grid as a parameter to a controller action through POST?

    
asked by RSillerico 24.11.2016 в 21:50
source

3 answers

2

By clicking on Listings what you should do is pass a code to the method. That is

    @Html.ActionLink("Listados", "listar", new { id = item.ID }) 

And then, in your controller method, you should have

    public ActionResult Listados(int id)
    {
        //Tu lógica aquí

        return View();
    }

Now, with regard to your request that this value does not pass through the URL, I do not see any problem. This is how ASP MVC works. Normally the actions of methods of type GET behave in this way.

On the other hand, it is not advisable to pass an entity or a model by means of a GET method. The ideal is to send a key, in this case your ID to then identify it within the method

Here is an example. Microsoft - Examining the Edit Methods and Edit View

    
answered by 24.11.2016 в 21:54
0

ActionLinks send the values in the url. So that it does not go through url you will have to do an ajax service and send it via post to the cs driver function.

I recommend you change your ActionLink by buttons. Let each button call a function that creates the object that contains the values you need to pass to the cs driver. This is where the ajax post comes in.

    
answered by 18.02.2017 в 02:03
0
public ActionResult listar(string dato)
{
    CTPP oTar  = new CTPP ();
    oTar.strCodEmpresa = "1";
    oTar.strNroTarjeta = dato;
    oTar.datFechaIni = DateTime.Parse("01/01/2016");
    oTar.datFechaFin = DateTime.Parse("12/12/2016");

    CLIS oLis = Servicioweb.listar(oTar);

    return View(oLis.lista);
}
    
answered by 07.12.2017 в 16:58