Redirecttoaction With Parameter

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



Leave a reply



Submit