The view appears for a second and then the following error gets thrown-
The parameters dictionary contains a null entry for parameter 'sortField' of non-nullable type 'Project.Enumerators.SortResultEnum' for method 'System.Threading.Tasks.Task1[System.Web.Mvc.ActionResult] Details(Project.Enumerators.SortResultEnum, System.Web.Helpers.SortDirection, System.String, System.Nullable1[System.Int32])' in 'Project.Controllers.SearchController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters
I have a form which is making an ajax call and later ajax call forwards it to controller.
Ajax call:
$(document).on('submit', '#SearchForm', function () {
var $form = $(this);
var $searchString = $("#SearchString").val();
var $sortField = $form.attr("data-sortOrder");
var $sortDirection = $form.attr("data-sortDirection");
SortResults($sortField, $sortDirection, $searchString);
});
Form:
<form id="SearchForm" action="@Url.Action("Details", "Search")" method="get" data-ajax="true" data-sortOrder="@SortResultEnum.ClientName" data-sortDirection="@SortDirection.Ascending">
@Html.TextBox("SearchString", ViewBag.CurrentSearchString as string, new { id = "SearchString", @class = "form-control", placeholder = "" })
<span class="input-group-btn">
<button class="btn btn-primary" type="submit" id="search-btn"><i class="glyphicon glyphicon-search"></i></button>
</span>
</form>
Controller Action is like
public async Task<ActionResult> Details(SortResultEnum sortField,SortDirection sortDirection, string searchString, int? page)
{
//code to call service and get result is here
// partial view is being used to display data
}
Can someone please help me with this error. Thanks.
sortFieldparameter. Debug your code to see why. Presumably$form.attr("data-sortOrder")doesn't produce a valid value. If it does, that value must be being lost somewhere. Debug to find out where. By the way, why are you prefixing your variables with$?sortField, not complete enum. You must replaceSortResultEnum sortFieldtostring sortField. You controller action method should have following parameterspublic async Task<ActionResult> Details(string sortField, string sortDirection, string searchString, int? page)SortResultEnum, so it should be declared as that type. The problem is that no such value is being passed.Enumtype parameter to controller. As suggested in following SO thread, enum values should be passed asintorstringand convert these values to enum types in controller.