简体   繁体   English

Ajax aspnet mvc 5 的局部视图

[英]Partial View with Ajax aspnet mvc 5

In my shared html I have a list of items which contains ids as names and I want to update my div when I pass that ID and I did make the post with AJAX but it doesn't update the div.在我共享的 html 中,我有一个包含 id 作为名称的项目列表,我想在传递该 ID 时更新我的 div,我确实使用 AJAX 发布了帖子,但它没有更新 div。

1 The list of companies: 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>

Them I call a partial view action to get the list of things:他们我调用部分视图操作来获取事物列表:

<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>

And the Action和行动

[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);
    }

and the View:和视图:

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>
    }

}

When I click in one of the items it does make the call, and goes inside the PartialView but does not update the div.当我单击其中一个项目时,它会进行调用,并进入 PartialView 但不更新 div。

<div id="teste">

        </div>

When you append data to div g is not defined you must write only当你 append 数据到 div g 没有定义你必须只写

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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