Checkbox values are always false in postback event through ajax. Please help………..
my view code is like this:
@using (Html.BeginForm("", "", FormMethod.Post, new { id = "frmCivilSurveys" })) {
<div class="ControlSet">@Html.LabelFor(model => model.SurfaceTypeId):
<div>@foreach (var item in Model.SurfaceTypeList) { @Html.Label(item.Text) @*<input data-val="true" id="@item.Text" name="@item.Text" style="margin-right: 200px; float: right;" type="checkbox" value="true" />*@ @Html.CheckBox(item.Text, new { style = "margin-right:
200px; float: right" })
}</div> </div> <div class="ControlSet">@Html.LabelFor(model => model.SurveyType):
@foreach (var item in Model.SurveyTypeList) { @Html.Label(item.Text) @*<input data-val="true" id="@item.Text" name="@item.Text" style="margin-right: 200px; float: right;" type="checkbox" value="true" />*@ @Html.CheckBox(item.Text, new { style = "margin-right:
200px; float: right" })
}</div>
<input type="submit" class="k-button" value="Create" style="float: right;" id="btnCreateCivilSurvey" />
ajax function responsible for posting back is :
$("#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 {
alert($("#frmCivilSurveys").validate().errorList[0].message);
$($("#frmCivilSurveys").validate().errorList[0].element).focus();
}
return false;
});
and controller function is
[HttpPost]
public ActionResult CivilSurveys(CivilSurveyViewModel modelData)
{
{
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();
}
}
what name did you give the checkbox? view the generated source to see. it must match the model property name. also:
var s = Request["SurveyAccepted"].Contains("true");
will not work because you posted json, not a form collection.
only the listbox created in foreach loop is not posting back the data.
if create check box using
@foreach (var item in Model.SurveyTypeList)
{
Html.Label(item.Text)
<input id="SurveyTypeList[@i].@item.Text" name="@item.Text" style="margin-right: 200px; float: right" type="checkbox" value="true" />
<br /><br />
i++;
}
but i get the data in postback if i use html like this.
@Html.LabelFor(model => model.SiteMeetReqLa):
<input data-val="true" id="SiteMeetReqLa" name="SiteMeetReqLa" style="margin-left: 62px;" type="checkbox" value="true">