我试图通过 jQuery Ajax 函数以 JSON 格式发布数据,这工作正常,但是我有布尔值并使用 html.checkBoxFor 即使我检查它也总是发布错误。我在 javaScript 中更新了代码,如果检查与否,我会相应地强制更改隐藏的输入值,我可以在调试中看到单击时发布 true 但在控制器端仍收到 false 的情况。在附加的屏幕截图中,您可以在控制器上看到正确的值,但不是错误的值
模型类属性
[Display(Name = "Secure Entire Property")]
[Required(ErrorMessage = "Require Information on If You Want to Secure Entire Property")]
public bool SecureEntireProperty { get; set; }
剃刀代码
<div class="form-group">
@Html.LabelFor(model => model._RentingApplicationModel.SecureEntireProperty, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@*@Html.EditorFor(model => model._RentingApplicationModel.SecureEntireProperty)*@
@Html.CheckBoxFor(model => model._RentingApplicationModel.SecureEntireProperty, new { @id = "SecureEntirePropertyCheck" })
@Html.ValidationMessageFor(model => model._RentingApplicationModel.SecureEntireProperty, "", new { @class = "text-danger" })
</div>
</div>
</div>
JavaScript
$("#SecureEntirePropertyCheck").click(function () {
if ($(this).is(':checked'))
{
$('[name="_RentingApplicationModel.SecureEntireProperty"]:hidden').val(true);
}
else
{
$('[name="_RentingApplicationModel.SecureEntireProperty"]:hidden').val(false);
}
});
我在哪里发布的 Jquery Ajax 方法
$.ajax({
url: formURL,
type: "POST",
dataType: "application/json",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({ TenentJSONList: AdditionalTenentList, ApplicationModelData: $("#CreateStudentRentingApplicationForm").serializeObject() }),
}).done(function (data, textStatus, jqXHR) {
//....my rest of code
附加租户列表
<script type="text/javascript">
var AdditionalTenentList = new Array();
$(".additionalTententUWLID").on("change", function () {
var StudentUWLID = $(this).val();
$(this).attr('id', StudentUWLID);
$.ajax({
url: '@Url.Action("GetStudentRecordByID", "StudentProfile")',
type: "POST",
dataType: "JSON",
data: { _GivenStudentUWLID: StudentUWLID },
cache: false
}).done(function (data, textStatus, jqXHR) {
if (data.RecordStatus == "NotAvailable")
{
alert("error");
}
else if(data.RecordStatus=="recordFound")
{
// alert(data.Response);
var paraedData = JSON.parse(data.Response);
AdditionalTenentList.push(StudentUWLID);
....my rest of code
</script>