Search form causing YSOD


I'm duplicating the search form for use in my navbar. This currently works in 1.6.1, but is creating a "Cannot perform runtime binding on a null reference" error in the current 1.x branch. Search form is being created as a shape and added to the Navigation zone via Layout.cshtml:
WorkContext.Layout.Navigation.Add(New.SearchNav(), "10");
SearchNav shape looks like this:
@using Orchard.Search.ViewModels;

@using (Html.BeginForm("index", "search", new { area = "Orchard.Search" }, FormMethod.Get, new { @class = "navbar-search pull-right" })) {
    @Html.TextBox("q", (SearchViewModel)Model.ViewModel.Query, new { @class = "search-query", placeholder = "Search" })
Removing (SearchViewModel)Model.ViewModel.Query and replacing with string.Empty resolves the issue.
Closed Jul 11, 2013 at 6:02 PM by BertrandLeRoy


Piedone wrote Jul 11, 2013 at 9:43 AM

This is not an issue with Orchard but rather with your code, thus it would be better in a discussion.

the problem is that you create a shape without a model injected but you're trying to access one with Model.ViewModel. You could inject the view model like this: New.SearchNav(ViewModel: yourObject)

psenechal wrote Jul 11, 2013 at 6:15 PM

Thanks for the update. You're absolutely correct now that I look at it. I'm not sure why this worked okay in 1.6 as it looks like it should have also produced the same error in that version as well. Perhaps the older version was a bit more forgiving. I'll update my code. Thanks!