MVC将表行数据传输到引导模型弹出窗口

时间:2018-08-15 05:07:40

标签: javascript c# asp.net-mvc-4 bootstrap-4 bootstrap-table

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

我能够使用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>

SampleController:-

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 ,因为它为null,Ajax调用失败。

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

谢谢。

1 个答案:

答案 0 :(得分:0)

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

当异常发生时,控制器动作应返回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?