how to return a view with querystring parameters in MVC5?

kaarthick raman picture kaarthick raman · Jul 19, 2016 · Viewed 21.7k times · Source

Very basic but not sure how to do since i am new to mvc.

I have to return a view based on a if condition.

if true, i should return a view with a guid value as querystring parameter else return a different view.

public ActionResult Act(Guid approvalCode)
    {
        bool result = businessProvider.CheckLinkValidity(approvalCode);
        if (result == true)
        {
            return View("Act"); //here i need to pass approvalcode as querystring param
          //want to do like
          //return View("Act"+"?code="+approvalcode) 
        }
        return View("LinkExpiredView");
    }

I need to render the view like:

~\ResetController\Act?code= someguidvalue

Answer

teo van kot picture teo van kot · Jul 19, 2016

I actually don't understand why you need query string.

Without it you can do like this:

return View("Act", new { code = "your-guid" });

If you still need it one of possible ways RedirectToAction method:

return RedirectToAction("Act", new { code = "your-guid" });