简体   繁体   English

ASP.NET MVC 代码模式弹出不发送参数到 controller

[英]ASP.NET MVC code modal popup not sending parameter to controller

I am trying to present Organization Details in a partial view in a Bootstrap Modal.我正在尝试在 Bootstrap Modal 的局部视图中显示组织详细信息。 After a day of reading/watching tutorials I am stumped.经过一天的阅读/观看教程后,我感到很困惑。

Organizations are loaded into a Table and there is a View option on each row.组织被加载到一个表中,每行都有一个视图选项。 The table has an id="OrganizationGrid"该表有一个id="OrganizationGrid"

 <table class="table table-striped table-bordered dt-responsive nowrap" width="100%" cellspacing="0" id="OrganizationGrid"> <thead> <tr style="color:#126E75; background-color:lightcyan"> <th style=" width:5%"> @Html.ActionLink("ID", "Index", new { sortOrder = ViewBag.IDSortParm, currentFilter=ViewBag.CurrentFilter }) </th> <th style=" width:25%"> @Html.ActionLink("Name", "Index", new { sortOrder = ViewBag.NameSortParm, currentFilter=ViewBag.CurrentFilter }) </th> <th style=" width:25%"> Parent Org </th> <th style=" width:10%; text-align:center"> Website </th> <th style="width:25%"> Comment </th> <th scope="col" colspan="3" style=" width:10%; text-align:center">Action</th> </tr> </thead> <tbody> @foreach (var item in Model) { <tr class=" table-light"> <td style="text-align:left"> @Html.DisplayFor(modelItem => item.Id) </td> <td style="text-align:left"> @Html.DisplayFor(modelItem => item.Name) </td> <td style="text-align:left"> @Html.DisplayFor(modelItem => item.ParentOrg.Name) </td> <td style="text-align: center"> <a href=@Html.DisplayFor(modelItem=> item.Website) target="_blank"> <i class="fa-solid fa-globe" target="_blank" rel="noopener noreferrer"></i> </a> </td> <td style="text-align:left"> @Html.DisplayFor(modelItem => item.Comment) </td> <td style="text-align: center"> <a asp-action="Edit" asp-route-id="@item.Id"> <i class="fas fa-edit"></i> </a> </td> <td> <a class="details" href="javascript:;">View</a> </td> <td style="text-align: center"> <a asp-action="Delete" asp-route-id="@item.Id"> <i class="fas fa-trash" style="color:red"></i> </a> </td> </tr> } </tbody> </table>

My understanding is that it triggers the JavaScript function which is currently residing at the bottom of the same page:我的理解是它触发了当前位于同一页面底部的 JavaScript function:

 <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <script type="text/javascript"> $(function() { $("#OrganizationGrid.details").click(function() { var organizationId = $(this).closest("tr").find("td").eq(0).html(); $.ajax({ type: "POST", url: "/Organizations/Details", data: '{organizationId: "' + organizationId + '" }', contentType: "application/json; charset=utf-8", dataType: "html", success: function(response) { $("#partialModal").find(".modal-body").html(response); $("#partialModal").modal('show'); }, failure: function(response) { alert(response.responseText); }, error: function(response) { alert(response.responseText); } }); }); }); </script>

I inserted a break in the controller to see if ajax was passing the organizationId but it is not.我在 controller 中插入了一个中断,以查看 ajax 是否传递了 organizationId,但事实并非如此。 This is the Controller Details Action method:这是 Controller 详细操作方法:

 [HttpPost]
    public ActionResult Details(int organizationId)
    {

        var organization = _context.Organizations
            .Include(o => o.ParentOrg).Where(p=>p.Id == organizationId);


        if (organization == null)
        {
            return NotFound();
        }
        return PartialView("_Details", organization);
    }

If I hard code organizationId in the Controller the popup loads with the correct information BUT if I hard code organizationId in the JavaScript function it still passes a null value to the controller. Could someone please point me in the right direction.如果我在 Controller 中对 organizationId 进行硬编码,弹出窗口会加载正确的信息但是如果我在 JavaScript function 中对 organizationId 进行硬编码,它仍然会将 null 值传递给 controller。有人可以指出我正确的方向吗?

The issue was with the JavaScript function and specifically the syntax of the data attribute in ajax. Corrected solution is as follows:问题出在 JavaScript function,特别是 ajax 中数据属性的语法。更正的解决方案如下:

 $(function () { $("#OrganizationGrid.details").click(function () { var organizationId = $(this).closest("tr").find("td").eq(0).html(); //alert(organizationId) $.ajax({ type: "POST", url: "/Organizations/Details", data: { 'id': organizationId }, //contentType: "application/json; charset=utf-8", //dataType: "html", success: function (response) { $("#viewEditOrgModal").find(".modal-body").html(response); $("#viewEditOrgModal").modal('show'); }, failure: function (response) { alert(response.responseText); }, error: function (response) { alert(response.responseText); } }); }); });

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

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