ASP.NET MVC: search in data entry form

and thanks for reading.

I am creating a data entry form. I’m trying to find a way to give the user the opportunity to provide criteria (last name for an example), find a table of employees for all employees that meet the criteria, display the result so that they can select the desired employee and go through the identifier of this employee back to the data entry form so that they can complete the recording and save it.

thanks

+4
source share
3 answers

One way to do this is with the jQuery autocomplete plugin . Have a text field in your form that allows you to search for a hidden field in which the identifier is stored. Use autocomplete through AJAX to get a list of name / identifier pairs returned as JSON based on your search criteria. Set autocomplete to force a selection from a list that prohibits any inconsistent text in the field. When the user selects an item from the list, the result function stores the corresponding identifier in a hidden field. Use the hidden field in the form message to get the employee ID.

It might look something like this:

View

$('#searchBox').autocomplete( '/Employees/Search', { dataType: 'json', max: 25, minChars: 2, cacheLength: 1, mustMatch: true, formatItem: function(data,i,max,value) { return value; }, parse: function(data) { var array = new Array(); for (var i=0; i < data.length; i++) { var datum = data[i]; var display = datum.FirstName + ' ' + datum.LastName; array[array.length] = { data: datum, value: display, result: display }; } } }); $('#searchBox').result( function(event, data, formatted) { if (data) { $('#employeeID').val( data.EmployeeID ); } }); $('form').submit( function() { if (!$('#employeeID').val()) { alert( 'You must select an employee before clicking submit!' ); return false; } }); <input type='text' id='searchBox' /> <input type='hidden' id='employeeID' name='employeeID' /> 

Controller:

 public ActionResult Search( string q, int limit ) { var query = db.Employees.Where( e => e.LastName.StartsWith( q ) ) .OrderBy( e => e.LastName ) .Select( e => new { FirstName = e.FirstName, LastName = e.LastName, EmployeeID = e.EmployeeID }); if (limit > 0) { query = query.Take(limit); } return Json( query.ToList() ); } public ActionResult SomeAction( int employeeID, ... ) { ... } 
+4
source

I suggest using Linq.

Think about Scott Gu's blog .........

http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx

 public static void GetEmployeeIDByLastName(string lastName) { DataContext dc = new DataContext(); var queryResult = from q in dc.Employee where q.EmployeeLastName.Equals(lastName) select new { EmployeeID = q.EmployeeID } foreach(var empID in queryResult) { //pass the empID value back to the display } } 
0
source

Something like this in the controller to search the database (using linq)?

 public ActionResult searchEmployees(string searchString) { var employees = (from e in db.Employees where e.Name.Contains(searchString) orderby e.Name select e); return view("SearchResult", employees); } 

EDIT: just read your comments, and if I understand correctly, you are only interested in the identifier. Do you use the ones in javascript and is this some kind of ajax call? In this case, you may need to return an array or csv string and process the identifier in a javascript call.

0
source

All Articles