Error 500 in Asp.Net Library Awesome

1

I found browsing this library ASP.net Awesome . I found it to be very functional and easy to use.

According to the documentation for using a helper named Lookup , you must have a controller and call the helper in cshtml in>. What happens is that, in my project, it is exactly the same as the documentation ( the code ), and I'm having an error 500 and I do not know exactly why.

I already researched and could not solve the error, could anyone help me?

  • Explaining Lookup does: It opens a popup with a list of what's in your table for you to select and populate a text field. You can search to find exactly what you want. And it is exactly in this Search method that is giving error, it would have to load all my records from the database, the names of the students. but it gives error 500 .

The controller code is this:

 
public class AlunoLookupController : Controller
{
    private EntidadesContext db = new EntidadesContext();
    //Aqui ele pega o item que foi selecionado e joga no campo de texto 
    public ActionResult GetItem(int? v)
    {
        var o = db.Alunos.SingleOrDefault(f => f.Id == v) ?? new Aluno();

        return Json(new KeyContent(o.Id, o.Nome));
    }
    // E aqui ele faz a busca dos registros no banco e exibe no popup
    //E aqui tabém dá erro, ele não acha nenhum registro e só fica carregando
    public ActionResult Search(string search, int page)
    {
        search = (search ?? "").ToLower().Trim();
        var list = db.Alunos.Where(f => f.Nome.ToLower().Contains(search));

        return Json(new AjaxListResult
        {
            Items = list.OrderBy(n => n.Nome).Skip((page - 1) * 7).Take(7).Select(o => new KeyContent(o.Id, o.Nome)),
            More = list.Count() > page * 7
        });
    }
}

I'm sure that problem is in the code I pulled from there, but I do not know what's going on.

The error that appears is as follows:

  

Description: An unhandled exception occurred during the execution of the current Web request. Examine the stack trace for more information about the error and where it originated in the code.

     

Exception Details: System.NotSupportedException: Only parameterless constructors and initializers are supported in LINQ to Entities.

    
asked by anonymous 19.05.2014 / 01:48

1 answer

4

Come on, I solved the problem. The problem is that I think the code is kind of old, and so it did not work well the way it is on the site. So I researched and used this reference here . what happens is that he was not able to connect with the bank to make the list, so I had to adapt the code, which is just below for anyone who wants to use the library too:

public class AlunoLookupController : Controller
{
    private EntidadesContext db = new EntidadesContext();
    public ActionResult GetItem(int? v)
    {
        var o = db.Alunos.SingleOrDefault(f => f.Id == v) ?? new Aluno();

        return Json(new KeyContent(o.Id, o.Nome));
    }

    public ActionResult Search(string search, int page)
    {
        search = (search ?? "").ToLower().Trim();
        var lista = db.Alunos.Where(f => f.Nome.Contains(search)).ToList();
        return Json(new AjaxListResult
        {
            Items = lista.OrderBy(f => f.Nome).Skip((page - 1) * 7).Take(7).Select(o => new KeyContent(o.Id, o.Nome)),
            More = lista.Count() > page * 7
        });
    }
}

Remembering that code above, it has to be in the controller that you want to manipulate the information, but outside the normal actions that are created, for example Home, it has to be below that action, but in the same class as below :

 public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

And here below is the controller code that ASP.net Awesome uses:

public class AlunoLookupController : Controller
{
    private EntidadesContext db = new EntidadesContext();
    public ActionResult GetItem(int? v)
    {
        //Os códigos das actions
    }
}
    
21.05.2014 / 15:20