Virtual Happy Hour this month, Jun 28, we'll be getting a sneak preview at our soon to launch SaaS CMS!

Try our conversational search powered by Generative AI!

MVC Routing

Vote:
 

I came across an issue where I am not able to navigate to the second page on HttpPost. I am not able to get the correct route values.

This is my HttpPost code. I tried different option to navigate to DeliveryController and action Index. 

public ActionResult CheckOut(CurrencyViewModel model)
{

// The below return statement, changes the url as /Delivery/Index?language=en and I am getting 404 error.
// return RedirectToAction("Index", "Delivery", new { language = ContentLanguage.PreferredCulture.Name });

// With the below return statement, the page simply refresh it.

// PageData startPage =
//ServiceLocator.Current.GetInstance().Get(ContentReference.StartPage);

// // get URL of the start page
// string startPageUrl = ServiceLocator.Current.GetInstance()
// .GetVirtualPath(startPage.ContentLink, startPage.LanguageBranch);

// return Redirect(startPageUrl);

// With the below return statement, the page simply refresh it.

return RedirectToAction("Index", new
{
language = ContentLanguage.PreferredCulture.Name,
node = ControllerContext.RequestContext.GetContentLink()
});

This is my CurrencyViewModel

public class CurrencyViewModel : BaseViewModel
{
public CurrencyPage CurrentPage { get; set; }

[Required]
public string Amount1 { get; set; }

[Required]
public string Amount2 { get; set; }
}

My currency index.cshtml has the below form and it is hitting the CurrencyController and action method checkout.

@using (Html.BeginForm("CheckOut", "Currency", new { language = ContentLanguage.PreferredCulture.Name, FormMethod.Post }))
{}

Can anyone explain me simple solution to implement the routing. 

#149601
Jun 05, 2016 5:59
Vote:
 

1. Get the url to the delivery page you want to redirect to using the UrlResolver.

var urlResolver = ServiceLocator.Current.GetInstance<UrlResolver>();
var url = urlResolver.GetUrl(deliverycontentReference, language);

2. Redirect to that url (avoid redirecting directly to the controller and action if you are not in the same controller). Redirect to a matching page instead and that call will end up in the right controller with correct route values etc

return Redirect(url);
#149602
Edited, Jun 05, 2016 9:57
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.