Ajax调用返回viewmodel,但是视图不显示结果

时间:2019-05-16 09:17:29

标签: javascript ajax asp.net-mvc model-view-controller asp.net-ajax

我正在尝试在局部视图中显示从方法返回的模型。调用使用Ajax进行,并且该方法返回viewModel。局部视图处于模式,因此单击按钮时,将调用该方法并显示局部视图。在局部视图中,如果我进行调试,我可以看到其中包含数据,但是当我遍历它时,它不会在@ item.Users.Email或任何users属性中显示结果。

我做错了什么?

这是代码

 [HttpGet]
    public PartialViewResult GetUsersForJob(int? id)
    {
        WhoBidForJobViewModel vm = new WhoBidForJobViewModel();
        var bids = (from j in _context.BidOfJobs where j.JobPostId == id       select j).ToList();
        List<ApplicationUser> users = new List<ApplicationUser>();
        foreach (var item in bids)
        {
            var user = (from u in _context.Users where u.Id ==        item.UserWhoBidId select u).First();
            users.Add(user);
        }
        vm.Users = users;

        return PartialView(vm);
    }

这是具有从Ajax调用的方法的控制器。

@model  FinalWorkFinder.Models.WhoBidForJobViewModel
   <ul class="interested-people">
@foreach (var item in Model.Users)
{
    <li>@Model.Users</li>
}

  </ul>

这是模态内部的局部视图。

$.ajax({
        url: '@Url.Action("GetUsersForJob", "Manage")',
        type: "GET",
        contentType: "JSON",
        cache:false,
        data: { id: idd },
        success: function (users) {
            $(".modal-title").html("Interested Users");


                console.log(users);

            }


    });

这是Ajax调用。

2 个答案:

答案 0 :(得分:0)

因此,经过长时间的聊天和讨论,结果是:

public PartialViewResult GetUsersForJob(int? id)
{
    WhoBidForJobViewModel vm = new WhoBidForJobViewModel();
    var bids = (from j in _context.BidOfJobs where j.JobPostId == id       select j).ToList();
    List<ApplicationUser> users = new List<ApplicationUser>();
    foreach (var item in bids)
    {
        var user = (from u in _context.Users where u.Id ==        item.UserWhoBidId select u).First();
        users.Add(user);
    }
    vm.Users = users;

    return PartialView("_partialView", vm);
}


$.ajax({
        url: '@Url.Action("GetUsersForJob", "Manage")',
        type: "GET",
        contentType: "JSON",
        cache:false,
        data: { id: idd },
        success: function (users) {
            $(".modal-title").html("Interested Users");

$("modal.selector.body").html("users");
                console.log(users);

            }


    });

答案 1 :(得分:0)

尝试一下并向我显示您idd的代码

$.ajax({
       url:'/GetUsersForJob/Manage',
        type: "GET",
        contentType: "JSON",
        cache:false,
        data: { id: idd },
        success: function (users) {
            $(".modal-title").html("Interested Users");


                console.log(users);

            }


    });