RedirectToAction with parameter
You can pass the id as part of the routeValues parameter of the RedirectToAction() method.
return RedirectToAction("Action", new { id = 99 });
This will cause a redirect to Site/Controller/Action/99. No need for temp or any kind of view data.
How to pass to RedirectToAction a parameter to another Action?
I removed:
return RedirectToAction (nameof (ExpertResult (), query);
I replaced:
return View ("ExpertResult", query);
RedirectToAction with parameters in Route
Probably, you are conflicting the area
parameter with area
route parameters. Areas is a resource from Asp.Net MVC with the following definition:
Areas are logical grouping of Controller, Models and Views and other
related folders for a module in MVC applications. By convention, a top
Areas folder can contain multiple areas. Using areas, we can write
more maintainable code for an application cleanly separated according
to the modules.
You could try to rename this parameter and use it, for sample, try to rename the area
to areaName
:
[Route("First")]
public ActionResult First()
{
return RedirectToAction("Second", new { areaName= "plans"});
}
[Route("Second/{areaName}")]
public ActionResult Second(string areaName)
{
return new ContentResult() { Content = "Second : " + areaName};
}
Can we pass model as a parameter in RedirectToAction?
Using TempData
Represents a set of data that persists only from one request to the
next
[HttpPost]
public ActionResult FillStudent(Student student1)
{
TempData["student"]= new Student();
return RedirectToAction("GetStudent","Student");
}
[HttpGet]
public ActionResult GetStudent(Student passedStd)
{
Student std=(Student)TempData["student"];
return View();
}
Alternative way
Pass the data using Query string
return RedirectToAction("GetStudent","Student", new {Name="John", Class="clsz"});
This will generate a GET Request like Student/GetStudent?Name=John & Class=clsz
Ensure the method you want to redirect to is decorated with
[HttpGet]
as
the above RedirectToAction will issue GET Request with http status
code 302 Found (common way of performing url redirect)
MVC redirecttoaction with parameter with Area
return RedirectToAction("Index", new { id = currentcoupon.Companyid.id, Area="Admin" });
Related Topics
When to Use .First and When to Use .Firstordefault With Linq
Creating a Dpi-Aware Application
What Does "Use of Unassigned Local Variable" Mean
Converting String to Byte Array in C#
Detect If Deserialized Object Is Missing a Field With the Jsonconvert Class in Json.Net
How to Get an Htmlelement Value Inside Frames/Iframes
How to Get Formatted Json in .Net Using C#
How to Use Reflection to Invoke a Private Method
Multidimensional Array [][] VS [,]
Why and How to Avoid Event Handler Memory Leaks
Json.Net Serialization of Type With Polymorphic Child Object
Get Enum from Description Attribute