BanksySan
BanksySan

Reputation: 28560

ReturnUrl is always null in MVC

I have a return url send after a user logs in:

e.g.

/Account/SignIn?ReturnUrl=%2fToDoItems%2fCreate

However, the value in the controller isn't binding.

[HttpPost]
[ActionName("SignIn")]
public ActionResult SignInConfirmation(UserCredentialsModel model, string returnUrl)
{
    if (ModelState.IsValid)

Also, the Request.QueryString is empty (bar the Length).

How do I bind the query string params to the controller?

As per below, I've tried capitalising the parameter name:

public ActionResult SignInConfirmation(UserCredentialsModel model, [Bind(Include = "ReturnUrl")] string ReturnUrl)

Upvotes: 0

Views: 564

Answers (1)

Win
Win

Reputation: 62300

You want to retrieve ReturnUrl from HttpGet method, and send it back on postback.

View

@using (Html.BeginForm("SignInConfirmation", "YOUR_CONTROLLER", 
      new { ReturnUrl = ViewBag.ReturnUrl }, 
      FormMethod.Post, new { role = "form" }))        
{
    ....
}

Controller

public ActionResult SignInConfirmation(string returnUrl)
{
   ViewBag.ReturnUrl = returnUrl;
   return View();
}

[HttpPost]
public ActionResult SignInConfirmation(UserCredentialsModel model, string returnUrl)
{
}

Upvotes: 2

Related Questions