ajax请求ASP.NET Core MVC上的未定义结果

问题描述 投票:0回答:1

这是我的Context类

public class UserInfoContext
{
    private readonly MVCDbContext _db;

    public UserInfoContext(MVCDbContext db)
    {
        _db = db;
    }

    public async Task<List<UserInformation>> GetAll()
    {
        var list = await _db.UserInfo.ToListAsync();
        return list;
    }

这是我的控制器

private UserInfoContext _ui_context;        
    public UserInformationController(UserInfoContext ui_context)
    {
        _ui_context = ui_context;
    }

    // GET: UserInformation
    public IActionResult Index()
    {
        return View();
    }

    [HttpGet]
    public async Task<IActionResult> LoadUserList()
    {
        var list = await _ui_context.GetAll();
        return new JsonResult(list);
    }

这是我的AJAX请求

<script>
$(document).ready(function () {

    loadUser();
    function loadUser() {
        $.ajax({
            method: "GET",
            url: "/UserInformation/LoadUserList",  
            data: {},
            dataType: "JSON",
            success: function (data) {       
                var html = '';
                $.each(data, function (key, item) {
                    html += '<tr>';
                    html += '<td>' + item.Id + '</td>';
                    html += '<td>' + item.FirstName + '</td>';
                    html += '<td>' + item.LastName + '</td>';
                    html += '<td>' + item.Location + '</td>';                      
                    html += '</tr>';
                });
                $('#table_user').html(html);
            }
        });
    }

});

这是我获得im getting undefined result的结果

这是登录调试器网络tabmy log on debugger

希望你先帮助我这个感谢!

asp.net-core-mvc asp.net-ajax
1个回答
0
投票

问题是您使用的是CAPITAL CASE。但你必须使用小写(item.iditem.firstName等)

 $.each(data, function (key, item) {
                    html += '<tr>';
                    html += '<td>' + item.id + '</td>';
                    html += '<td>' + item.firstName + '</td>';
                    html += '<td>' + item.lastName + '</td>';
                    html += '<td>' + item.location + '</td>';                      
                    html += '</tr>';
                });
© www.soinside.com 2019 - 2024. All rights reserved.