繁体   English   中英

Kendo HTML.TextAreaFor将不会更新绑定到的数据库

[英]Kendo HTML.TextAreaFor will not update the data base it is bound to

这是一个问题:使用TextAreaFor时,触发事件更新时,数据不会更新数据库,因此看起来好像它已更改,直到刷新页面或从页面移开然后返回到页面为止。

这是视图上的代码。

<div class="panel panel-default panel-body">
    <div class="col-lg-1">
        <label for="Id">Id</label>
        <br />
        <br /> @Html.ValueFor(m => m.Id)
    </div>
    <div class="col-lg-3">
        <label for="questionText">Question Text</label>
        <br /> @Html.TextAreaFor(m => m.QuestionText, new { cols = 28, rows = 4, @class = "form-control" })
    </div>
    <div class="col-lg-1">
        <label for="answerId">Correct Answer Id</label>
        <br /> @Html.TextBoxFor(m => m.AnswerId, new { @class="form-control", style="width:70px;"})
    </div>
    <div class="col-lg-7 ">
        <label for="answerExplanation">Answer Explanation</label>
        <br /> @Html.TextAreaFor(m => m.AnswerExplanation, new { @class = "form-control", cols = 75, rows = 4 })
    </div>
</div>
<div class="panel panel-body panel-info">
    <div class="row" style="padding-top:10px;">
        <div class="col-sm-2">
            <button id="btnApprove" class="btn btn-danger">Update/Approve</button>
        </div>
        <div class="col-sm-4">
            <button id="btnCancel" class="btn btn-success">Cancel Edit</button>
        </div>
        <div class="col-sm-4 right">
            @Html.CheckBox("ckApprove") Approve on move
        </div>
        <div class="col-sm-2">
            <button id="btnPrev" type="button" class="btn btn-primary" title="Previous Question"><span class="glyphicon glyphicon-arrow-left"></span></button>
            <button id="btnNext" type="button" class="btn btn-primary" title="Next Question"><span class="glyphicon glyphicon-arrow-right"></span></button>
        </div>

这是用于移动或按钮更新的脚本

<script>
    var questionId = @Model.Id;
    var nextQuestionId = parseInt(@Model.NextQuestionId);
    var prevQuestionId = parseInt(@Model.PrevQuestionId);
    var autoApprove = true;

    function getQuestionId() {
        return {
            questionId: questionId
        };
    }


    $(document).ready(function() {
        autoApprove = ("@ViewBag.AutoApprove".toLowerCase() === "true");
        nextQuestionId = parseInt(@Model.NextQuestionId);
        prevQuestionId = parseInt(@Model.PrevQuestionId);
        console.log(@Model.NextQuestionId + "/" + prevQuestionId);

        if (nextQuestionId != 0) {
            $("#btnNext").removeClass("disabled");
        } else {
            $("#btnNext").addClass("disabled");
        }
        if (prevQuestionId != 0) {
            $("#btnPrev").removeClass("disabled");
        } else {
            $("#btnPrev").addClass("disabled");
        }

        $("#ckApprove").prop('checked', autoApprove);

        $("#answerGrid").removeClass("k-widget");

        $("#btnApprove").click(function() {
            updateQuestion();
        });

        $("#btnCancel").click(function() {
            window.location = "/backendQA"
        });

        $("#btnPrev").click(function() {
            // if checkbox is checked then updateQuestion()
            if ($("#ckApprove").is(':checked')) {
                updateQuestion();
                autoApprove = true;
            } else {
                autoApprove = false;
            }
            window.location = "/BackendQA/Questions?testId=" + @ViewBag.TestId + "&questionId=" + @Model.PrevQuestionId + "&autoApprove=" + autoApprove;
        });


        $("#btnNext").click(function() {
            // if checkbox is checked then updateQuestion()
            if ($("#ckApprove").is(':checked')) {
                updateQuestion();
                autoApprove = true;
            } else {
                autoApprove = false;
            }
            window.location = "/BackendQA/Questions?testId=" + @ViewBag.TestId + "&questionId=" + @Model.NextQuestionId + "&autoApprove=" + autoApprove;
        });

    });

    function updateQuestion() {
        // set autoApprove = is the checkbox checked?
        var test = @ (Html.Raw(Json.Encode(Model)));

        test.Id = $("#Id").val();
        test.questionText = $("#QuestionText").val();
        test.answerId = $("#AnswerId").val();
        test.answerText = $("#AnswerExplanation").val();

        console.log(JSON.stringify(test));

        $.ajax({
            url: "/BackendQA/Question_Update",
            data: JSON.stringify(test),
            method: "POST",
            contentType: 'test/json',
            complete: function(x, status) {
                var result = JSON.parse(x.responseText);
            },
            error: function(jq, status, e) {
                $("#message").html("<p style=\"color: red;\">An error occurred. Please contact support with the following information:</p>" + jq.status + "<br />" + jq.responseText + "<br />" + e);
            }
        })
    }

这是控制器代码:

public ActionResult Question_Update([DataSourceRequest]DataSourceRequest request, QaQuestionModel test)
    {
        if (ModelState.IsValid)
        {
            ModelState.Clear();
            QaQuestions tests = new QaQuestions(test.Id);
            return Json(tests.Update(test), JsonRequestBehavior.AllowGet);
        }
        return Json(ModelState.ToDataSourceResult());
    }

还有一个带有内联编辑的网格,该网格也不会更新。

您发送了一个POST,但该操作似乎是为GET服务的。 也许它甚至没有调用。 在操作方法中应用[HttpPost]属性。

[HttpPost]
public ActionResult Question_Update([DataSourceRequest]DataSourceRequest      request, QaQuestionModel test)
{
    if (ModelState.IsValid)
    {
        ModelState.Clear();
        QaQuestions tests = new QaQuestions(test.Id);
        return Json(tests.Update(test));
    }
    return Json(ModelState.ToDataSourceResult());
}

暂无
暂无

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

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