【问题标题】:Ajax POST data not appearing in modalAjax POST 数据未出现在模式中
【发布时间】:2019-10-15 03:10:48
【问题描述】:

我有一个由 JSON 数据填充的表。在最后一列中,您可以打开与每一行对应的模式,您可以在输入框中添加注释——这样做将更新该模式。换句话说,没有 cmets 的表格行具有空模态,并且具有至少一个注释的行应该出现在它们对应的模态中。

在我的后端管理器(SQL Server Management Studio)中,我可以看到 cmets 显示在那里,但它们没有出现在模态中。

我不知道问题是来自 JavaScript 端(视图)还是来自模型,还是来自其他地方。

DocBudgetId 的添加相对较新。在此之前,评论/评论数据与 DocNumber 相关联,并且信息出现在模式中。我怀疑我必须对 DocBudgetId 进行调整/更改,但我不确定如何。

模态截图: https://i.stack.imgur.com/Vqd4o.png DocNumber 出现在左上角。已对该模式所属的行进行了注释,但它们未出现在模式表中。

型号:

public class ReviewRow
    {
        public Guid DocBudgetId { get; set; }
        public string DocNumber { get; set; }
        public string ReviewBy { get; set; }
        public string ReviewOn { get; set; }
        public string ReviewComment { get; set; }   
    }

查看:

<div class="modal-body">
        <div id="review"></div>
        <table>
           <tr>
              <td>
                @Html.LabelFor(x => Model.ReviewComment)<br />
                @Html.TextAreaFor(x => x.ReviewComment, new { style = "width: 200px; " })<br />
                @Html.HiddenFor(x => x.DocUnderReview, new { id = "txtDocUnderReview" })
                <input type="submit" value="@BB360.Models.BudgetReportingViewModel.Buttons.Review" name="@Html.NameFor(x => x.SubmitButton)" id="SubmitButton" class="btn btn-secondary" />
              </td>
           </tr>
        </table>


// --- other code --- //

function ShowReviewModal(DocBudgetId, DocNumber) { 
        $("#txtDocUnderReview").val(DocNumber);
        console.log(DocNumber)

        $.ajax({
            type: "POST",
            url: "/Doc/GetDocUnderReviewDetails",
            data: "docNumber=" + DocNumber + "&docBudgetId=" + DocBudgetId,
            timeout: 5000,
            success: function(data) {
                console.log(data); // ------------- empty array
                // write out the modal table
                var tBody = $("#tblExpenseDeductionDetails tbody");
                tBody.empty();
                s = "";

                for (x = 0; x < data.length; x++) {
                  let ReviewBy = data[x].ReviewBy,
                      ReviewOn = data[x].ReviewOn,
                      ReviewComment = data[x].ReviewComment;
                        s = s + "<tr>";
                        s = s + "<td>" + ReviewBy + "</td>";
                        s = s + "<td>" + ReviewOn + "</td>";
                        s = s + "<td title='" + ReviewComment.replace(/'/g, '') + "' style='max-width:550px;word-wrap: break-word;'>" + stringTruncate(ReviewComment, 65) + "</td>";
                        s = s + "</tr>";
                }

                $("#modalDocName").text(DocNumber);
                tBody[0].innerHTML = s;
                $("#ReviewModal").modal();
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(errorThrown);
            }
        });

        $("#ReviewModal").modal();
    }

控制器

public ActionResult BudgetReporting(BudgetReportingViewModel model)
        {
            Common.GetDbExecutor().QueryNonQuery(Sql.InsertDocUnderReview, new {
                model.DocBudgetId, 
                DocNumber = model.DocUnderReview, // D is uppercase
                ReviewBy = Common.GetUserName(),
                ReviewComment = model.ReviewComment,
            }, 30).ToString();

            PopulateBudgetReportingDetail(model);

            return View(model);
        }

        public ActionResult GetDocUnderReviewDetails(Guid docBudgetId) // camelCase // 
        {
            var data = Common.GetKKDbExecutor().Query<BudgetReportingViewModel.ReviewRow>(Sql.GetDocUnderReviewDetails, new {
               docBudgetId
            }).ToList();

            return Json(data);
        }

Sql文件

namespace BB360
{
    public partial class Sql
    {
        public const string GetDocUnderReviewDetails = @"

        select 
            DocBudgetId, 
            DocNumber, 
            ReviewBy, 
            convert(varchar, ReviewOn, 101) as ReviewOn, 
            isnull(ReviewComment, '') as ReviewComment 
            from DocBudgetReview 
            where DocBudgetId = @DocBudgetId
            order by ReviewOn desc   
        ";
    }
}

【问题讨论】:

  • 不确定它是否相关,但是为什么要发送 docNumber 和 docBudgetId,因为服务器端只需要一个 docBudgetId
  • 在您的控制器方法中,您正在接受 Guid 参数并且您正在将数据作为字符串传递,因此将 Guid 设置为字符串。

标签: javascript c# sql ajax


【解决方案1】:

这里的问题是您的“GetDocUnderReviewDetails”操作接受了 GET 请求,并且您正在向它发送一个发布请求。您只需将 ajax 请求类型更改为 GET。

type: "GET",
url: "/Doc/GetDocUnderReviewDetails",

我注意到的另一件事是,您也在您的操作不需要的请求中发送 docNumber。即使这不会导致任何问题,但您不需要在 ajax 中发送它

【讨论】:

  • 嗨@manvinder-singh。这绝对是一个 POST 请求(即使 url 名称中有 Get),并且 DocNumber 出现在模式的顶部,因此可以轻松识别。 DocNumber 正确显示在那里。
【解决方案2】:

更新:我做了一些修改并得到了表格数据来显示。

在存储过程(GetDocUnderReviewDetails.cs)中,我注释掉了where DocBudgetId = @DocBudgetId这一行并添加了where DocNumber = @DocNumber。我注意到,如果我只是注释掉 where DBI =,那么它会显示为每个模态生成的 所有 cmets,这是我之前处理的一个问题。

所以现在,我的存储过程如下所示:

namespace BB360
{
    public partial class Sql
    {
        public const string GetDocUnderReviewDetails = @"

        select 
            DocBudgetId, 
            DocNumber, 
            ReviewBy, 
            convert(varchar, ReviewOn, 101) as ReviewOn, 
            isnull(ReviewComment, '') as ReviewComment 
            from DocBudgetReview 
            --where DocBudgetId = @DocBudgetId
            where DocNumber = @DocNumber
            order by ReviewOn desc   
        ";
    }
}

我还在控制器中添加了string docNumber

public ActionResult GetDocUnderReviewDetails(Guid docBudgetId, string docNumber)
        {
            var data = Common.GetBBDbExecutor().Query<BudgetReportingViewModel.ReviewRow>(Sql.GetDocUnderReviewDetails, new {
                docBudgetId,
                docNumber
            }).ToList();


            return Json(data);
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-20
    • 1970-01-01
    • 2012-02-21
    • 1970-01-01
    • 1970-01-01
    • 2016-10-22
    • 1970-01-01
    相关资源
    最近更新 更多