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

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


我可以在 Javascript 中使用 row[] 数组选择行列值,但我无法通过 Ajax 将其传输到我的控制器。


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


<html>

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


样本控制器:-


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 调用失败。


谁能指导我如何实现这一目标?


倚天杖
浏览 196回答 1
1回答

30秒到达战场

AJAX 回调不打算执行重定向到另一个操作方法(也data使用字符串连接分配参数data: '{ID: "' + param + '" }'肯定是错误的)。尝试配置 AJAX 回调以查看错误消息是否存在,如下所示:$.ajax({&nbsp; &nbsp; &nbsp; type: "GET",&nbsp; &nbsp; &nbsp; url: '@Url.Action("_SampleAction", "SampleController")',&nbsp; &nbsp; &nbsp; data: { ID: param }, // proper way to pass string to controller with AJAX&nbsp; &nbsp; &nbsp; // other AJAX settings&nbsp; &nbsp; &nbsp; success: function (result) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (result.message == undefined) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#myModelContent').html(result);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#myModal").modal('show');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // set redirection here instead of using RedirectToAction from controller&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; window.location.href = result.url;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; error: function (req, status, error) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;alert("Error occurred");&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });});并且控制器动作应该在异常发生时返回 JSON 数据,如下所示:public ActionResult _SampleAction(string ID){&nbsp; &nbsp; MyModel objMyModel = new MyModel();&nbsp; &nbsp; objMyModel.ID = ID;&nbsp; &nbsp; try&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // do something&nbsp; &nbsp; &nbsp; &nbsp; return PartialView("_SampleAction", objMyModel);&nbsp; &nbsp; }&nbsp; &nbsp; catch (Exception ex)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return Json(new { url = Url.Action("Index", "Error"), message = ex.Message }, JsonRequestBehavior.AllowGet);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP