0

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.

9
  • The error message is telling you that your action is not receiving a value for tis sortField parameter. 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 $? Commented May 25, 2018 at 4:39
  • You are passing enum value to controller sortField, not complete enum. You must replace SortResultEnum sortField to string sortField. You controller action method should have following parameters public async Task<ActionResult> Details(string sortField, string sortDirection, string searchString, int? page) Commented May 25, 2018 at 4:40
  • @mmushtaq, that's not correct. The parameter is supposed to receive a value of type SortResultEnum, so it should be declared as that type. The problem is that no such value is being passed. Commented May 25, 2018 at 4:42
  • The correct value is being received in $form.attr("data-sortOrder"). My form appears for a second and then gets lost and the error appears. I have checked the model being passed in PartialView has all the correct values Commented May 25, 2018 at 4:48
  • @jmcilhinney I wonder how OP can pass Enum type parameter to controller. As suggested in following SO thread, enum values should be passed as int or string and convert these values to enum types in controller. Commented May 25, 2018 at 4:50

1 Answer 1

1

I think you missed "" in data-sortOrder. Try this.

  data-sortOrder="@SortResultEnum.ClientName"
Sign up to request clarification or add additional context in comments.

3 Comments

What difference will it make? I am getting the right value in the model that I am passing to PartialView. The partialView appears for a second and then gets disappeared and the error message appears
ReplaceResults(response); Never used or seen this function before. Is it default function or your own???
I just made it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.