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

I would like to pass multiple objects using the redirectToAction () method. The following is the action I'm redirecting to.

public ActionResult GetEmployees(Models.Department department, Models.Category category, Models.Role role) { return View(); } 

I would like to do something like 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

Update

Is it possible to 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 }); 
+4
source share
2 answers

You cannot pass objects to the RedirectToAction method. This method is intended to pass only parameters. Therefore, you need to pass all the values ​​that you want to correct 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 the best way is to only send the identifiers of these objects:

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

and then in the target controller action, use these identifiers to retrieve entities from your base dataset:

 public ActionResult GetEmployees(int depId, int catId, int roleId) { var dep = repository.GetDep(depId); var cat = repository.GetCat(catId); var role = repository.GetRole(roleId); ... } 
+16
source

This is not an answer in itself, but I always see similar questions that come down to a fundamental misunderstanding or misunderstanding of how HTTP works. This is not criticism, many web developers believe that all they need to know is HTML, JavaScript and CSS to create a website, but they neglect the need to understand the platform on which their code runs. Look at it this way: you won’t sit down and start writing an application without understanding the target platform (Windows, Mac, iOS, Android, etc.). You must know how everyone handles memory, garbage collection, storage, etc., in order to write anything that could be anything.

The same applies to the network. It is a distributed platform, but a platform nonetheless, and it is important to understand how the underlying structure works. Obviously, I will not dwell on this here in detail. There are whole volumes on HTTP and related technologies. For more information, I highly recommend compiling something like O'Reilly HTTP: the ultimate guide .

As for your problem here, HTTP implements various "verbs", the most common of which are GET and POST. Simply put, GET is a non-volatile request for the returned resource, while POST is volatile (changes will be made, resources will be deleted, etc.). There is no body request with GET. A request can consist of various parts, a URL, headers and a body. In POST, the published data will be the body of the request, but GET has no data and, therefore, is not the object of the request. Now, again, we speak here simplistically. You may wonder about querystring. Will it be "published data"? Technically, yes, it can be, but again, technically, anything in the URL (or if we really want to be exact, a URI) is part of the identifying data for an existing resource. For example, if you do a search on Google, your search query will be added to the URI for the search results page. This is the published data (you sent the request), but this is not only data, but also the URI with the query string gives the location of the resource corresponding to this exact request. Someone who entered the same request will be sent to the same URL.

This was a little tangent, but it’s important to understand that a query is not a way to transmit unrelated data. This line is part of the URI, so the exact page loaded by two different requests are two completely different resources.

Upon transition, redirection is not a special type of request (in the sense of being represented by another HTTP verb); it’s just an instruction for the client’s browser to issue another GET request to the specified URL. You do not control which verb is used: it is always GET. Thus, you can not miss anything for the trip. If you have objects that will be represented by a redirected URI, then, obviously, you will pass the identification information necessary to obtain them (id, slug, etc.), Using either the URI path and / or the sequence of requests. If any data is not directly related to the presented resource, this data should go to some other type of storage system, such as a session.

+2
source

All Articles