Reputation: 741
So, when I click a button - it hits submit. I manage to get the URL value fine. My issue is that hitting "RedirectingTime" function won't redirect the page. It just stops and displays a blank page. If I put it inside the Submit function then it works fine but I don't want to change my submit from a void to a redirect as I would like to also return a view if it errors. Thanks!
[HttpPost]
public void Submit(URLModel model)
{
string url = EmailEnd(model);
if (url != "0")
{
RedirectingTime(url);
}
else
{
Error();
}
}
public RedirectResult RedirectingTime(string url)
{
return Redirect(url);
}
public ActionResult Error()
{
return View();
}
Based on the answer below - the working code is:
[HttpPost]
public ActionResult Submit(URLModel model)
{
string url = EmailEnd(model);
if (url != "0")
{
return Redirect(url);
}
else
{
return View("Error");
}
}
}
Upvotes: 0
Views: 1114
Reputation: 10818
The crux of your problem is that your Submit action is not returning anything. It has to return an ActionResult (or a derived form of ActionResult). If you want to "Redirect" to another action on your controller, you use RedirectToAction
. You can use this with or without parameters as illustrated below:
[HttpPost]
public ActionResult Submit(URLModel model)
{
string url = EmailEnd(model);
if (url != "0")
{
return RedirectToAction("RedirectingTime", new { url = url });
}
else
{
return RedirectToAction("Error");
}
}
public RedirectResult RedirectingTime(string url)
{
return Redirect(url);
}
public ActionResult Error()
{
return View();
}
Upvotes: 1
Reputation: 239360
The problem is your Submit
action returning void
. That will always result in a blank page, because void is essentially the same as EmptyResult
.
The action you hit is what returns the result, not some action you call from it. Even though RedirectingTime
returns a redirect, your Submit
action never returns that, so the result is still EmptyResult
and not RedirectResult
.
Also, for what it's worth, it's atypical and unnecessary to explicitly set the type of the return value of the action. Virtually every MVC action signature should simply have ActionResult
as the return. You can actually return anything with that, RedirectResult
, ViewResult
, JsonResult
, etc.
Upvotes: 1