简体   繁体   中英

Convert a LINQ result into a list and return it to view using AJAX (ASP.NET MVC)

I have an ASP.NET MVC project, when I use Ajax to get a list from action, nothing is returned and I get error in ajax.

This is the code and the question is how can I return Linq result from my action to the ajax as a list and read it in ajax

Ajax code:

$.ajax({
                    type: 'POST',
                    url: '@Url.Action("XXX")',
                    datatype: JSON,
                    data: { 'UF_ID': $("#UF").val() },
                    success: function (data) {
                        $("#TBL").empty();
                        alert(data);
                    },
                    error: function () {
                        alert('error');
                    }
                });

Action code

public JsonResult XXX(string UF_ID)
{
    SCHOOLmEntities context = new SCHOOLmEntities();

    List<FRMTR> Lis =new List<FRMTR>();

    var R = from x in context.FRMTRs 
            join y in context.UFs on x.CIN_FRMTR equals y.FRMTR_UF 
            where y.CD_UF == UF_ID 
            select x;

    Lis = R.ToList<FRMTR>();

    return Json(Lis, JsonRequestBehavior.AllowGet);
}

I need the returned list in Ajax and read its properties also in ajax

This is the solution in my DBCONTEXT:

public MyDbContext()   : base("name=MyDbContext"){this.Configuration.ProxyCreationEnabled = false;}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM