how to return a view with querystring parameters in MVC5?
Asked Answered
S

2

5

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

Synchro answered 19/7, 2016 at 6:44 Comment(1)
Instead of query string, you can set the value of your GUID into viewbag and access it your view. ViewBag.GuidVariable = Guid_Value;Vizza
I
12

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" });
Isologous answered 19/7, 2016 at 6:51 Comment(2)
thanks for the response. But RedirectToAction doesn't work.Synchro
This is the best answerCullender
S
0

You can also use ViewBag to set some value https://msdn.microsoft.com/en-us/library/system.web.mvc.controllerbase.viewbag(v=vs.118).aspx

Selfindulgent answered 20/7, 2016 at 13:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.