tags:

views:

661

answers:

3

Hello, and thanks for reading.

I am building a data entry form. I am trying to figure out a way to let the user to provide a criteria (last name for instance), search the employee table for all employees that match the criteria, display the result in a way they can select the correct employee, and pass the the ID of that employee back to the data entry form so they can complete the record and save it.

Thanks

A: 

I suggest using Linq.

Consider Scott Gu's Example 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
        }
}
Goober
Thanks for your reply.I know how to do the search at the controller level, my problem is the user interface. From the view where i am entering data, i need to provide a way to go and search for the employee, select one from all that match the criteria and go back to the data entry form passing the EmployeeID of the selected employee. I am using LINQ to SQL right now.
Luis
Goober
A: 

Something like this in the controller to search in 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're only interested in the id's. Are you using those in javascript, and is this some kind of ajax call ? In that case you might want to return an array, or a csv string and deal with the id's in the javascript call.

Morph
thanks for the reply. That looks like what i got. Going to try and understand tvanfosson example. The autocomplete sounds like very intuitive for the user. Too bad I am just starting out as a dev and dont really know any javascript, heck i am just learning c# !!
Luis
I have a blogpost at http://tinyurl.com/ckp63n you can take a look at.
Morph
+1  A: 

One way to do this would be with the jQuery autocomplete plugin. Have text box on your form that allows search and a hidden field that stores the id. Use autocomplete via AJAX to get a list of name/id pairs returned as JSON based on the search criteria. Have the autocomplete set up to force selection from the list -- which will disallow any non-matching text in the field. When the user selects an item from the list have the result function store the associated id in the hidden field. Use the hidden field on form post to get the ID of the employee.

It might look something like:

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, ... )
{
   ...
}
tvanfosson
Thank you.This sounds like a great solution. I am going to read up a bit on JavaScript at least enough to try and assemble your example. It looks like I am way over my head here heh! Thanks again.
Luis
I was hoping something as simple as this sounds, would be easily accomplished by using only asp.net mvc and a .net language, but it seems javascript is unavoidable to get any work done past the demos and tutorials.
Luis
You could do this using only MVC but you'd have to have a view for the search, then a view for the search results, then go back to the original view to input the data given the selected employee. If you can live with the javascript solution, it will be a better user experience. You might want to think about how you'd handle it if javascript isn't enabled, though. It might not be too hard to handle both just in case. For an intranet app though, where you have more control over the browser, that might not be an issue.
tvanfosson
Thanks again, its an intranet app and we have control over the browser. I agree, the autocomplete its ideal, I just need to learn enough JS to understand it. I tried to implement it last night from home to no avail, I guess there are a few things still missing from my arsenal. Going to give it a whirl today again.
Luis