Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass multiple objects using RedirectToAction() in Asp.NET MVC?

I would like to pass multiple objects using redirectToAction() method. Below is the actionresult i'm redirecting to.

public ActionResult GetEmployees(Models.Department department, Models.Category category, Models.Role role)
        {  

return View();
}

I'd like to do something like the below

public ActionResult test()
{
Models.Department dep = new Models.Department();
Models.Category cat.......etc

return RedirectToAction("GetEmployees", dep, cat, role); }

Any help would be greatly appreciated - thanks

Updated

Can I use something like

 Models.Department dep = new Models.Department() { DepId = employee.DepartmentId };
                    Models.Category cat = new Models.Category() { CatId = employee.JobCategoryId };
                    Models.Role title = new Models.Role() { RoleId = employee.JobTitleId };

            return RedirectToAction("GetEmployees", new { department = dep, category = cat, role = title });
like image 945
Ren Avatar asked Apr 01 '13 13:04

Ren


2 Answers

You cannot pass objects to the RedirectToAction method. This method is designed to pass only parameters. So you will need to pass all the values you want to be emitted in the corresponding GET request:

return RedirectToAction("GetEmployees", new 
{
    DepId = dep.DepId,
    DepName = dep.DepName,
    CatId = cat.CatId,
    RoleId = role.RoleId,
    ... so on for each property you need
});

But a better way is to only send the ids of those objects:

return RedirectToAction("GetEmployees", new 
{
    DepId = dep.DepId,
    CatId = cat.CatId,
    RoleId = role.RoleId
});

and then in the target controller action use those ids to retrieve the entities from your underlying datasore:

public ActionResult GetEmployees(int depId, int catId, int roleId)
{
    var dep = repository.GetDep(depId);
    var cat = repository.GetCat(catId);
    var role = repository.GetRole(roleId);
    ...
}
like image 199
Darin Dimitrov Avatar answered Oct 16 '22 11:10

Darin Dimitrov


This is not an answer, per se, but I see questions similar to this all the time that boil down to a fundamental misunderstanding or lack of understanding about how HTTP works. This is not a criticism, many web developers think that all they need to know is HTML, JavaScript and CSS to make a website, but they neglect to see the need to actually understand the platform their code is running on. Look at it this way: you wouldn't sit down and start writing an app without understanding your target platform (Windows, Mac, iOS, Android, etc.). You'd have to know how each handles memory, garbage collection, storage, etc., in order to write anything that would amount to anything.

The same applies to the web. It's a distributed platform, but a platform nonetheless, and it's important to understand how the underlying structure works. I obviously won't go into extreme detail on this here. There's entire volumes on HTTP and associated technologies. For more information, I highly recommend picking up something like O'Reilly's HTTP: The Definitive Guide.

As for your problem here, HTTP implements various "verbs", the most common of which are GET and POST. Simplistically, GET is a non-volatile request for a resource to be returned, while POST is volatile (changes will be made, resources deleted, etc.). With GET there is no request "body". A request can be composed of various parts, a URL, headers, and a body. In a POST, the posted data would constitute the request body, but GET does not have posted data and therefore no request body. Now, again, we're speaking simplistically here. You might wonder about the querystring. Would that not be "posted data"? Technically, yes, it can be, but again, technically, anything in the URL (or if we really want to be exact, the URI) is a piece of identifying data for an existing resource. When you make a search on Google, for example, your search query will be appended to the URI for the search results page. This is posted data (you posted the query), but it's not just data, the URI with the query string gives the location of the resource that corresponds to that exact query. Someone else who entered the same query would be sent to the same URL.

That was a bit of a tangent, but it's important to understand that a querystring is not a way to pass unrelated data. The querystring is part of the URI so the exact page loaded with two different querystrings is two entirely different resources.

Moving on, a redirect is not some special type of request (in the sense of being represented by a different HTTP verb); it's merely an instruction to the client's browser that it should issue another GET request to the specified URL. You don't get any control over what verb is used: it's always GET. So, you can't pass anything along for the ride. If you have objects that will be represented by the URI being redirected to, then obviously you would pass the identifying information required to retrieve them (id, slug, etc.) using either the URI path and/or querystring. If there's any data not directly related to the resource being represented, that data must go in some other type of storage system, such as the session.

like image 34
Chris Pratt Avatar answered Oct 16 '22 10:10

Chris Pratt