Data between views with ASP.NET

0

Hello I need to pass a data from one view to another, but I can not get it. Someone could please tell me the most appropriate way to send data. In this case my data is being obtained from another function and I need to know how to pass it is a int that I occupy as id

This is the view of which I want to send my data

//vista index
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

   @Html.ActionLink("Check Request", "Details", "Request", new { id = 123 })

I need to go through this control and return it in the view Details

    [Authorize]
    public ActionResult Details(int id)
    {
        ViewBag.Id = id;
        return View();
    }
@{
    ViewBag.Title = "Detalles - Cotización";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

   <h1> Tu Id es: @ViewBag.Id </h1>

    
asked by E.Rawrdríguez.Ophanim 19.12.2017 в 17:53
source

2 answers

1

What you are missing are the attributes for the html in this case would be null it would stay that way - >

@Html.ActionLink("Check Request", "Details", "Compra", new { id = 123 },null)

and with that you can send the int parameter as you have it or if you want to add the attributes because you put it like this:

@Html.ActionLink("Check Request", "Details", "Compra", new { id = 123 },new {@class="btn btn-primary" })
    
answered by 20.12.2017 / 17:49
source
2

The first thing is not to confuse the temporary information that passes between requests, if you are going to use ViewBag you should use it both in the View and in the Controller, it is not the best practice to use TempData and ViewBag .

For the passing of parameters the correct thing would be to send them as part of the fourth parameter that is of type IDictionary<string, object> , for example:

@Html.ActionLink("Check Request", "Details", "Request", new { id = 123 })

Which, the Controller will receive that data as a parameter in the method and in turn, we keep it in the temporary dictionary ViewBag to be able to send it to the view Details :

[Authorize]
public ActionResult Details(int id)
{
    ViewBag.Id = id;
    return View();
}

In the view Details , you can read it in the same way making use of ViewBag , that is, as an example if you want to use it within an Html element:

<h1> Tu Id es: @ViewBag.Id </h1>
    
answered by 19.12.2017 в 18:06