视图出现一秒钟,然后抛出以下错误 -
参数字典包含参数' sortField'的空条目。非可空类型' Project.Enumerators.SortResultEnum'方法' System.Threading.Tasks.Task 1[System.Web.Mvc.ActionResult] Details(Project.Enumerators.SortResultEnum, System.Web.Helpers.SortDirection, System.String, System.Nullable
1 [System.Int32])'在' Project.Controllers.SearchController'。可选参数必须是引用类型,可空类型,或者声明为可选参数。
参数名称:参数
我有一个正在进行ajax调用的表单,后来ajax调用将它转发给控制器。
Ajax电话:
$(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 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就像
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
}
有人可以帮我解决这个错误。感谢。
答案 0 :(得分:1)
我认为你在data-sortOrder中错过了“”。 试试这个。
data-sortOrder="@SortResultEnum.ClientName"