简体   繁体   中英

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.

1 The list of companies:

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

<div id="teste">

        </div>

When you append data to div g is not defined you must write only

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

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