0

I am following the tutorial:

http://www.codeproject.com/Articles/762959/jQuery-UI-Autocomplete-in-MVC-Selecting-Nested-Ent

I blocked at the code

public JsonResult GetListForAutocomplete(string term)
{               
    Person[] matching = string.IsNullOrWhiteSpace(term) ?
    db.Persons.ToArray() :
    db.Persons.Where(p => p.LastName.ToUpper().StartsWith(term.ToUpper())).ToArray();

return Json(matching.Select(m => new
{
     id = m.Id, 
     value = m.LastName, label = m.ToString() 
}), JsonRequestBehavior.AllowGet);
}

I have error on db variable that is not declared. But I don't know how to declare it. The tutorial doesn't mention it.

carmi
  • 1
  • 1

1 Answers1

0

It looks like db is supposed to be a dbcontext variable. You just have to declare it and instantiate it up at the top of your controller. Then you can use it in any method/action of that controller that you need to. A dbcontext is basically just setting up the link to your database.

I think it's slightly confusing because the author never mentions there is a database behind his solution, but it looks like there is.

So it will look something like this:

public class TestController : Controller
{

    private MyProjectEntities db = new MyProjectEntities();
Josh Blade
  • 981
  • 2
  • 10
  • 24