I know that it is possible to do this using Request.Form.GetValues

Here is the view

  <div class="input-group"> <input name="searchString" type="text" id="searchString" class="form-control"/> <span class="input-group-btn"> <button class="btn btn-default" type="button" value="Search" id="btnSearch" >Поиск</button> </span> </div> 

Looks like a controller

 var searchString = Request.Form.GetValues("searchString").FirstOrDefault(); if (!string.IsNullOrEmpty(searchString)) { activeDirectory = activeDirectory.Where(a => a.DisplayName != null && a.DisplayName.Contains(searchString) || a.Company != null && a.Company.Contains(searchString)); } 

But when you start, an error occurs:

Anonymous of type 'System.ArgumentNullException' occurred in System.Core.dll but it was not handled in user code

Additional information: Value may not be null.

How do I solve this problem? Data loading goes, through JSON I need to do this using jquery.

Now at startup, an error occurs screenshot enter image description here

  • Data can be obtained by name from the form, that is, if there is <input type = "text" name = "searchString" /> then you can pass the string as an input parameter of the method - Vladimir Paliukhovich

3 answers 3

The name of the property Request.Form is confusing - in fact, all the values ​​obtained from the body of the POST request fall here. If you make a GET request or POST request, but pass the value through the query string, then Request.Form.GetValues("searchString") will be empty, hence the error.

The easiest way is to take searchString as an action parameter (see Vladimir’s answer) - then it can be obtained from both sources.

  • But what about all the same using Request.Form.GetValues ​​to get data? - shatoidil
  • @shatoidil you are doing right, the error is elsewhere. - Pavel Mayorov
  • @shatoidil unless I would use the Request.Form indexer ["searchString"] - Pavel Mayorov
  • I have attached a screenshot. Rather, I think I do not correctly refer to this input. Any thoughts on this? - shatoidil
  • @shatoidil read my answers again - Pavel Mayorov

Parameters can be passed as:

 public void TestMethod(string searchString) { } <form action="http://localhost:53013/Gameplay/TestMethod"> <input name="searchString"> <button type="submit">Submit</button> </form> 

    It is possible without specifying the searchString parameter in the method to get the value via Request.Params , while checking whether the string is defined and not empty:

     var searchString = string.IsNullOrEmpty(Request.Params["searchString"]) ? "" : Request.Params["searchString"];