以下是问题:当使用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());
}
还有一个内联编辑的网格也不会更新。
答案 0 :(得分:0)
您发送了一个POST,但该操作似乎是为GET服务的。也许它甚至没有打过电话。在action方法中应用[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());
}