使用Ajax aspnet mvc 5的部分视图

时间:2019-11-04 03:18:37

标签: c# ajax asp.net-mvc

在我的共享html中,我有一个包含ID作为名称的项目的列表,我想在传递该ID时更新div,我确实使用AJAX撰写了帖子,但它不会更新div。

1公司列表:

<div class="dropdown-menu dropdown-menu-right shadow animated--grow-in" aria-labelledby="userDropdown">
                            @{
                                foreach (var item in List)
                                {
                                    <a class="dropdown-item" name="@item.CompanyToken" id="selector">
                                        <i class="fas fa-user fa-sm fa-fw mr-2 text-gray-400" id="CompanyId"></i>
                                        @item.CompanyName
                                    </a>
                                }
                            }
                        </div>

我调用局部视图操作来获取事物列表:

<script>
      $(document).ready(function(){
      $('.dropdown-item').click(function(e){
          e.preventDefault();
          $.ajax({
            url: '@Url.Action("GetProcess", "Manager")',
            type: "get",
            dataType: "html",
            contentType: 'application/json; charset=utf-8',
            data: { id: $(this).attr('name') }, //add parameter
            success: function (data) {
                //success
                $('#teste' + g).html(data); //populate the tab content.   
            },
            error: function () {
                alert("error");
            }
        });
      })
    })
</script>

还有动作

[HttpGet]
    public async Task<PartialViewResult> GetProcess(string id)
    {
        TempData["CompanyToken"] = id;
        var value = await Context.Company.Include(a => a.ProcessDefinition).Where(a => a.CompanyToken == id).Select(a => a.ProcessDefinition).SingleOrDefaultAsync();
        return PartialView("GetProcessDefinitions", value);
    }

和视图:

foreach (var item in Model)
{
    if (item != null)
    {
        var words = GenerateRandomWords.RandomString(6);
        <li class="nav-item">
            <a class="nav-link collapsed" href="#" data-toggle="collapse" data-target="#@words" aria-expanded="true" aria-controls="collapseTwo">
                <i class="fas fa-align-justify"></i>
                <span>@Html.DisplayFor(modelItem => item.Nome)</span>
            </a>
            <div id="@words" class="collapse" aria-labelledby="headingTwo" data-parent="#accordionSidebar">
                <div class="bg-white py-2 collapse-inner rounded">
                    <h6 class="collapse-header">Insatancias:</h6>
                    <a class="collapse-item" href="@Url.Action("", "",new { id = item.Id })">a</a>
                    <a class="collapse-item" href="@Url.Action("", "",new { id = item.Id })">a</a>
                    <a class="collapse-item" href="@Url.Action("", "",new { id = item.Id })">a</a>
                    <a class="collapse-item" href="@Url.Action("", "",new { id = item.Id })">a</a>
                    <a class="collapse-item" href="@Url.Action("", "",new { id = item.Id })">a</a>
                    <a class="collapse-item" href="@Url.Action("", "",new { id = item.Id })">a</a>
                    <a class="collapse-item" href="@Url.Action("", "",new { id = item.Id })">a</a>
                    <a class="collapse-item" href="@Url.Action("", "",new { id = item.Id })">a</a>
                </div>
            </div>
        </li>
    }

}

当我单击其中一项时,它会进行调用,并进入PartialView内,但不会更新div。

<div id="teste">

        </div>

1 个答案:

答案 0 :(得分:0)

当您将数据附加到div g时未定义 您只能写

$('#teste').html(data);