[英]checkboxfor bind to model ajax postback
我試圖通過我的控制器中的Ajax回發來獲取復選框,以便檢查值,但是即使在單擊按鈕控件之前在視圖中選中了復選框,它也給我帶來了錯誤。
我的代碼如下所示:
[HttpPost]
public ActionResult CivilSurveys(CivilSurveyViewModel modelData)
{
try
{
var s = Request["SurveyAccepted"].Contains("true");
if (modelData.SurveyAccepted)
{
modelData.SurveyAcceptedBy = int.Parse(Session["SessionUserId"].ToString());
modelData.SiteDiagram = ViewBag.Image;
modelData = (BAL.Surveys.GetCivilSurvey(modelData));
}
return View(modelData);
}
catch (Exception ex)
{
Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
return View();
}
}
視圖
@using (Html.BeginForm("", "", FormMethod.Post, new { id = "frmCivilSurveys" }))
{
<div class="ControlSet">
@Html.LabelFor(model => model.SurveyAcceptedBy):
@Html.CheckBoxFor(model => model.SurveyAccepted, new { style = "margin-left:198px;" })
</div>
<div style="clear: both; margin: 10px; padding-top: 10px; overflow: hidden;">
<input type="submit" class="k-button" value="Create" style="float: right;" id="btnCreateCivilSurvey" />
<input type="button" class="k-button" value="Update" id="btnEditCivilSurvey" style="float: right;" />
</div>
<script>
$("#btnCreateCivilSurvey").click(function() {
if ($("#frmCivilSurveys").valid()) {
$.ajax({
type: "POST",
url: "@Url.Action("CivilSurveys", "Civils")",
data: JSON.stringify($('#frmCivilSurveys').serializeObject()),
dataType: "json",
contentType: "application/json; charset=utf-8",
async: true,
cache: false,
success: function (msg) {
if (msg.status == "1") { }
alert(msg.message);
}
});
} else {
$($("#frmCivilSurveys").validate().errorList[0].element).focus();
}
return false;
});
</script>
這是因為使用checkboxfor時會自動創建隱藏字段。
我已經通過使用類型為checkbox的輸入並手動在foreach循環中為他們手動設置id來解決此問題,如下所示:
@foreach (var item in Model.SurveyTypeList)
{
@Html.Label(item.Text)
<input name="SurveyTypeList[@i].Text" type="hidden" value="@item.Text" />
<input name="SurveyTypeList[@i].Value" type="hidden" value="@item.Value" />
<input data-val="true" id="SurveyTypeList@(i)__IsSelected" name="SurveyTypeList[@i].IsSelected" style="margin-right: 200px; float: right" type="checkbox" value="true" />
<br /><br />
i++;
}
但這不是一個完美的解決方案,我需要一些更完美的解決方案
這是因為使用checkboxfor時會自動創建隱藏字段。
我已經通過使用類型為checkbox的輸入並手動在foreach循環中為他們手動設置id來解決此問題,如下所示:
@foreach (var item in Model.SurveyTypeList)
{
@Html.Label(item.Text)
<input name="SurveyTypeList[@i].Text" type="hidden" value="@item.Text" />
<input name="SurveyTypeList[@i].Value" type="hidden" value="@item.Value" />
<input data-val="true" id="SurveyTypeList@(i)__IsSelected" name="SurveyTypeList[@i].IsSelected" style="margin-right: 200px; float: right" type="checkbox" value="true" />
<br /><br />
i++;
}
但這不是一個完美的解決方案,我需要一些更完美的解決方案
將您的ajax調用更改為
$.ajax({
type: "POST",
url: "@Url.Action("CivilSurveys", "Civils")",
data: $('#frmCivilSurveys')),
// dataType: "json", - Note you appear to be returning a view so this should be html
cache: false,
success: function (msg) {
....
關鍵點
.serialize()
而不是serializObject()
JSON.stringify
contentType: "application/json; charset=utf-8"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.