lovettjay
asked on
MVC DDL Json Not Binding to Model
I am trying to post the data from JSON into the save action method but not having any luck. When I inspect element I see the data and also see it populate the DDL. However the data(questions_id) is not saving during the post. I am not sure how to bind the questions_id value that is being return in the JSON action results.
Model
Controller
Index
Model
public int exams_questions_id { get; set; }
public Nullable<int> exams_id { get; set; }
public Nullable<int> questions_id { get; set; }
public Nullable<int> credentials_id { get; set; }
public Nullable<int> concerns_id { get; set; }
public Nullable<short> exams_questions_order { get; set; }
public string exams_questions_string { get; set; }
public string exams_questions_value { get; set; }
public virtual concern concern { get; set; }
public virtual credential credential { get; set; }
public virtual exam exam { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<exams_questions_choices> exams_questions_choices { get; set; }
public virtual question question { get; set; }
Controller
public ActionResult Create(int id, int credentials, int questions_id)
{
exams_questions exams_questions = new exams_questions();
exams_questions.exams_id = id;
exams_questions.credentials_id = credentials;
ViewBag.concerns_id = new SelectList(db.concerns, "concerns_id", "concerns_description");
ViewBag.credentials_id = new SelectList(db.credentials, "credentials_id", "credentials_code");
ViewBag.exams_id = new SelectList(db.exams, "exams_id", "exams_description");
ViewBag.questions_id = new SelectList(db.questions, "questions_id", "questions_string");
return View(exams_questions);
}
public JsonResult GetQuestionByID(int concerns_id)
{
db.Configuration.ProxyCreationEnabled = false;
return Json(db.exams_questions.Where(x => x.concerns_id == concerns_id), JsonRequestBehavior.AllowGet);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "exams_questions_id,exams_id,questions_id,credentials_id,concerns_id,exams_questions_order,exams_questions_string,exams_questions_value")] exams_questions exams_questions)
{
if (ModelState.IsValid)
{
db.exams_questions.Add(exams_questions);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.concerns_id = new SelectList(db.concerns, "concerns_id", "concerns_description", exams_questions.concerns_id);
ViewBag.credentials_id = new SelectList(db.credentials, "credentials_id", "credentials_code", exams_questions.credentials_id);
ViewBag.exams_id = new SelectList(db.exams, "exams_id", "exams_description", exams_questions.exams_id);
ViewBag.questions_id = new SelectList(db.questions, "questions_id", "questions_string", exams_questions.questions_id);
return View(exams_questions);
}
Index
<div class="form-group">
@Html.LabelFor(model => model.concerns_id, "concerns_id", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(x => x.concerns_id, ViewBag.concerns_id as SelectList, "Select Concern", new { id = "concerns_id" })
@Html.ValidationMessageFor(model => model.concerns_id, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.questions_id, "questions_id", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<select id="Q" class="form-control"></select>
@Html.ValidationMessageFor(model => model.questions_id, "", new { @class = "text-danger" })
</div>
</div>
<script>
$(function () {
$("#concerns_id").change(function () {
$.get("http://localhost/NAHPapp/ExamCreation/GetQuestionByID", { concerns_id: $("#concerns_id").val() }, function (data) {
$("#Q").empty();
$.each(data, function (index, row) {
$("#Q").append(" <option value='" + row.concerns_id + "'>" + row.questions_id + "</option>")
});
})
});
});
</script>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER