1

I want to pass a ViewModel to a Sitecore Page from a Controller Action.

So far this below post Link has helped, but I'm not able to pass a model to the page.

I'm calling the controller from a Form post.

 @using (Html.BeginForm())
          {
              @Html.Sitecore().FormHandler("Search", "Index")
              <li class="nav-item"><a class="nav-link" href="@Model.ScheduleLinkUrl" data-fancyscroll="data-fancyscroll"> <span class="nav-link-text">Schedule</span></a></li>
              <li class="nav-item"><input type="text" id="searchBox" /><button type="submit">Search</button></li>
          }

And inside controller, what I want to have is:

var url = LinkManager.GetItemUrl(Sitecore.Context.Database.GetItem(new ID("{E2C750EE-7F7D-4A9E-983E-8F04C8CC8B6B}")));
return Redirect(url, model);

Can anyone help me out on this? Thanks, community for the help.

4
  • 1
    If i understood correctly your question you are trying an approach similar to the one described stackoverflow.com/questions/55734601/… or mhwelander.net/2014/05/22/…? Mar 11 at 13:13
  • Thanks Cristi. So I can use the below code to redirect to the page with a viewmodel? return RedirectToRoute(Sitecore.Mvc.Configuration.MvcSettings.SitecoreRouteName, new { pathInfo = pathInfo.TrimStart(new char[] { '/en/Home/NewPage' }), carID = id }); } }
    – Suman Baul
    Mar 11 at 13:31
  • Yes, similar to that, depending on your case. If it works will add it as an answer :) Mar 11 at 13:51
  • @Cristy Yes it works. Thanks, man you are a lifesaver! :)
    – Suman Baul
    Mar 11 at 14:28
3

Since we cannot use RedirectToAction() in Sitecore MVC as it interrupts the page rendering process, in this case you can use the RedirectToRoute(), for example using this approach:

var item = Sitecore.Context.Database.GetItem(Sitecore.Data.ID.Parse("[your item id]"));
var pathInfo = LinkManager.GetItemUrl(item, UrlOptions.DefaultOptions);
return RedirectToRoute(Sitecore.Mvc.Configuration.MvcSettings.SitecoreRouteName, new { pathInfo = pathInfo.TrimStart(new char[] { '/' }), carID = id });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.