Reputation: 89
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.Nullable
1[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.
Upvotes: 0
Views: 106
Reputation: 65
I think you missed "" in data-sortOrder. Try this.
data-sortOrder="@SortResultEnum.ClientName"
Upvotes: 1