MVC 将 Table 行数据传输到 bootstrap 模型弹出窗口

MVC transfer Table row data to bootstrap model popup

我正在使用 bootstrap-table 库在我的 ASP.net MVC 视图中创建一个 table。 每当用户双击 table 行时,要求选择 table 行数据并将其传输到模型弹出窗口。

我可以在 Javascript 中使用 row[] 数组选择行列值,但我无法通过 Ajax.

将其传输到我的控制器

请在下面找到我的代码片段:-

<html lang="en-AU">
<head>
<script>

$(document).ready(function () {

$('#tblID').bootstrapTable({ locale: 'en-US' }).on('click-row.bs.table', function (e, row, $element) {

var param = row[2]; //fetching 3rd column value

$.ajax({
      type: "GET",
      url: '@Url.Action("_SampleAction", "SampleController")',
      data: '{ID: "' + param + '" }',
      datatype: "json",
      success: function (result) {
      $('#myModelContent').html(result);
      $("#myModal").modal('show');
      },
      error: function (req, status, error) {
           alert("Error!Occured");
      }
    });

});

});

</script>
</head>

<body>

<table id="tblID" class="table table-hover" data-search="true" data-show-columns="true" data-mobile-responsive="true" data-striped="true" data-toggle="table" data-sort-name="Salary" data-sort-order="desc" data-pagination="true" data-smart-display="true" data-filter-control="true" data-show-export="true" data-click-to-select="true" data-page-list="[5, 10, 25, 50, 100, ALL]" data-export-options='{"fileName": "ActiveList","ignoreColumn": ["state"]}'>

    <thead>
            <tr>
                @foreach (DataColumn col in Model.Details.Columns)
                {
                    <th class="TableRowHeader" data-sortable="true" data-filter-control="Select">@col.ColumnName</th>
                }
            </tr>
    </thead>
    <tbody>
            @foreach (DataRow row in Model.MonitorDetails.Rows)
            {
                <tr>
                    @foreach (DataColumn col in Model.MonitorDetails.Columns)
                    {
            <td class="TableBody">@row[col.ColumnName]</td>
            }
                </tr>
            }
        </tbody>
</table>

    <div class="modal fade" tabindex="-1" id="myModal" data-keyboard="false" data-backdrop="static">
        <div class="modal-dialog">
            <div class="modal-content" style="width: auto;">
                <div id='myModalContent'></div>
            </div>
        </div>
    </div>

</body>
</html>

示例控制器:-

public ActionResult _SampleAction(string ID)
    {
        MyModel objMyModel  = new MyModel ();
        objMyModel.ID = ID;
        try
        {
            return PartialView("_SampleAction", objMyModel);

        }
        catch (Exception ex)
        {
            return RedirectToAction("Index", "Error");
        }
    }

但我收到的参数 ID 为空,Ajax 调用失败。

任何人都可以指导我如何实现这一目标吗?

提前致谢。

AJAX 回调不是为了执行重定向到另一个操作方法(也分配 data 带有字符串连接的参数,如 data: '{ID: "' + param + '" }' 绝对是错误的)。尝试配置 AJAX 回调以查明是否存在这样的错误消息:

$.ajax({
      type: "GET",
      url: '@Url.Action("_SampleAction", "SampleController")',
      data: { ID: param }, // proper way to pass string to controller with AJAX

      // other AJAX settings

      success: function (result) {
          if (result.message == undefined) {
              $('#myModelContent').html(result);
              $("#myModal").modal('show');
          } else {
              // set redirection here instead of using RedirectToAction from controller
              window.location.href = result.url;
          }
      },
      error: function (req, status, error) {
           alert("Error occurred");
      }
    });
});

控制器动作应该return JSON异常发生时的数据,如下所示:

public ActionResult _SampleAction(string ID)
{
    MyModel objMyModel = new MyModel();
    objMyModel.ID = ID;
    try
    {
        // do something

        return PartialView("_SampleAction", objMyModel);
    }
    catch (Exception ex)
    {
        return Json(new { url = Url.Action("Index", "Error"), message = ex.Message }, JsonRequestBehavior.AllowGet);
    }
}

相关问题:

RedirectToAction not working after successful jquery ajax post?