5

I have action method in controller called "Registration" as

public ActionResult Facility(int id = 0, int contractId = 0)

when I call this method from url like

/Registration/Facility/0?contractId=0

it works fine. Now when I try to construct above url in another method like

return RedirectToAction("Facility/0?contractId="+ model.ContractId);

it doesn't work, the url in browser is not constructed well it comes like

/Registration/Facility/0%3fcontractId%3d0

can anyone please tell me what wrong I'm doing here?

pramodtech
  • 6,300
  • 18
  • 72
  • 111

3 Answers3

11

Try this:

return RedirectToAction("Facility", new { id = 0, contractId = model.ContractId});

See this answer

Community
  • 1
  • 1
jao
  • 18,273
  • 15
  • 63
  • 96
  • thanks jao, it worked. But can you tell me why my code is not working, is there any rule that we have to use New{....} for constructing url? – pramodtech Apr 27 '11 at 06:38
  • 3
    Your code is not working because that overload of the method automatically escapes the ? and / characters so that you don't have to (it assumes that if you wanted to pass parameters, you'd use the overload which accepts parameters). Remember, the parameter is not an URL, it is an action name. Use Redirect if you want to supply a URL – Martin Booth Apr 27 '11 at 06:41
  • that's well explained.....it's my ignorance, i could have checked the method signature before. Anyway, thanks a lot. – pramodtech Apr 27 '11 at 06:56
1

There is a built in method overload for redirecting. Pass in an anonymous object with the values you want

return RedirectToAction("Facility", new { id = 0, contractId = model.ContractId });
Martin Booth
  • 8,485
  • 31
  • 31
0

You have to pass the parameters like below:

return RedirectToAction("Facility", new { contractId = model.ContractId });
Kamyar
  • 18,639
  • 9
  • 97
  • 171