0

I am calling a controller action from a form post, fetching all the data inside the controller and passing the model inside View Rendering, but Im getting null values inside the View.

I have used this link, to Model type inside my CMS. Screenshot below: enter image description here

Controller Code:

public ActionResult Index()
{
    IEnumerable<SearchResultItem> data = null;
    Item item = Sitecore.Context.Database.GetItem(new ID("{EEE7110D-6A46-4A20-B6EB-60FAC94B42D2}"));
    var resultSet = new EventSearchList();
    using (var context = ContentSearchManager.CreateSearchContext(new SitecoreIndexableItem(item)))
    {
        var query = context.GetQueryable<SearchResultItem>().Where(x => x.Content.Contains("Sitecore PowerShell Extensions"));
        var results = query.GetResults();
        if (results != null && results.TotalSearchResults > 0)
        {
            data = results.Hits.Where(i => i.Document != null).Select(i => i.Document);
            

            foreach (var items in data)
            {
                var resultItem = items.GetItem();
                var eventSearchItem = new EventSearchItem()
                {
                    Title = resultItem.Fields["Title"].Value,
                    BodyCopy = resultItem.Fields["Body Copy"].Value
                };
                
                resultSet.eventSearchItems.Add(eventSearchItem);
            }
        }
    };
     var pathInfo = Sitecore.Links.LinkManager.GetItemUrl(Sitecore.Context.Database.GetItem(new ID("{E2C750EE-7F7D-4A9E-983E-8F04C8CC8B6B}")));
            return RedirectToRoute(Sitecore.Mvc.Configuration.MvcSettings.SitecoreRouteName, new { pathInfo = pathInfo.TrimStart(new char[] { '/' }), resultSet });
        }
    }
}

View Model

namespace Linde.Feature.Search.Models
{
     public class EventSearchList
     {
        public List<EventSearchItem> eventSearchItems = new List<EventSearchItem>();         
     }
}

View:

@using Sitecore.Mvc
@using Sitecore.Mvc.Presentation
@model Linde.Feature.Search.Models.EventSearchList

@if (Model != null)
{
    <table>
    @foreach (var item in Model.eventSearchItems)
    {
        <tr>
            <td>Employee name:</td><td style="color:orange">@item.Title</td>
            <td>Years of work:</td><td style="color:orange">@item.BodyCopy</td>
        </tr>
    }
</table>
}

Can anyone point out what am I doing wrong here? Thanks a lot for the help!

1

The problem is that RedirectToRoute works as standard HTTP GET with URL params appended by "?" to your URL, therefore, you can't pass complex models between your actions, only simple types.

I am not sure the exact context of the Index() action, is it possible to move the search logic to the other action instead and only pass the search parameter instead? I would recommend exploring the possibility of implementing the functionality of displaying the results using ajax, that way a redirect won't be necessary.

Otherwise in order to pass a list of complex objects like above you would need to use other solutions like MVC TempData, similar to how it is described in this post https://stackoverflow.com/questions/1789351/sharing-viewdata-across-actions-in-asp-net-mvc

Update: Adding the retrieved model into Session does the work for now.

3
  • Hi Cristi, tried your last option, even with tempdata and viewdata, the model is not getting passed into the view. It is still null. – Suman Baul Mar 15 at 18:22
  • You could also use Cache/Session to pass data, but those need to be cleared afterwards. It is unclear for me why you would need to redirect. Instead of having a View Rendering you can just add another action in your Controller that handles the HTTPPost and will return a custom view with your model. You can take a look at the answer posted here sitecore.stackexchange.com/questions/7893/… – Cristi Vulturar Mar 15 at 18:39
  • Im using Redirect to Route as I want the search results to be displayed in a new Sitecore Page(Item) with the search results. Working with session gives me the desired output and Im able to display it to my page. This is though not a standard practice, I need to modify the existing code. @Cristi, it would help me immensely if you can throw some articles or links that can help me with a standard practice for implementing Search inside Sitecore 10. And again thanks a log for your help. – Suman Baul Mar 16 at 6:32

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.