0
尋找一些幫助將AJAX值(members_exams_proctorname)綁定到模型中,以便在發佈期間保存。學校放下工作,並將獲得監考人名字的價值。當我檢查元素時,我可以看到監督員的姓名。Casecade DropDownList w/Ajax綁定到模型
型號
public int members_exams_id { get; set; }
public Nullable<int> members_id { get; set; }
public Nullable<short> schools_id { get; set; }
public Nullable<int> exams_id { get; set; }
public Nullable<int> members_exams_status_id { get; set; }
public Nullable<int> members_exams_types_id { get; set; }
public string members_exams_username { get; set; }
public string members_exams_password { get; set; }
public string members_exams_firstname { get; set; }
public string members_exams_middlename { get; set; }
public string members_exams_lastname { get; set; }
public string members_exams_ssn { get; set; }
public string members_exams_email { get; set; }
public Nullable<System.DateTime> members_exams_expirationdate { get; set; }
public Nullable<System.DateTime> members_exams_examdate { get; set; }
public Nullable<byte> members_exams_session { get; set; }
public Nullable<decimal> members_exams_passingscore { get; set; }
public Nullable<decimal> members_exams_score { get; set; }
public Nullable<System.DateTime> members_exams_startdate { get; set; }
public string members_exams_address { get; set; }
public string members_exams_city { get; set; }
public string members_exams_state { get; set; }
public string members_exams_zip { get; set; }
public string members_exams_phone { get; set; }
public string members_exams_testinglocation { get; set; }
public string members_exams_proctorname { get; set; }
public Nullable<bool> members_exams_application { get; set; }
public Nullable<bool> members_exams_payment { get; set; }
public Nullable<bool> members_exams_diploma { get; set; }
public Nullable<bool> members_exams_processing { get; set; }
控制器
public ActionResult NewTryDDL()
{
List<SelectListItem> schoolsId = new List<SelectListItem>();
members_exams members_exams = new members_exams();
List<school> sch = db.schools.ToList();
sch.ForEach(x =>
{
schoolsId.Add(new SelectListItem { Text = x.schools_name, Value = x.schools_id.ToString() });
});
;
ViewBag.schools_id = schoolsId;
ViewBag.exams_id = new SelectList(db.exams, "exams_id", "exams_description");
ViewBag.members_exams_types_id = new SelectList(db.members_exams_types, "members_exams_types_id", "members_exams_types_description");
ViewBag.members_exams_status_id = new SelectList(db.members_exams_status, "members_exams_status_id", "members_exams_status_description");
return View(members_exams);
}
[HttpPost]
public ActionResult GetContact(string schools_id)
{
int schoolId;
List<SelectListItem> members_exams_proctorname = new List<SelectListItem>();
if (!string.IsNullOrEmpty(schools_id))
{
schoolId = Convert.ToInt32(schools_id);
List<schools_contacts> contact = db.schools_contacts.Where(x => x.schools_id == schoolId).ToList();
contact.ForEach(x =>
{
members_exams_proctorname.Add(new SelectListItem { Text = x.schools_contacts_firstname, Value = x.schools_contacts_firstname.ToString() });
});
}
return Json(members_exams_proctorname, JsonRequestBehavior.AllowGet);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult NewTryDDL([Bind(Include = "members_exams_id,members_id,schools_id,exams_id,members_exams_status_id,members_exams_types_id,members_exams_username,members_exams_password,members_exams_firstname,members_exams_middlename,members_exams_lastname,members_exams_ssn,members_exams_email,members_exams_expirationdate,members_exams_examdate,members_exams_session,members_exams_passingscore,members_exams_score,members_exams_startdate,members_exams_address,members_exams_city,members_exams_state,members_exams_zip,members_exams_phone,members_exams_testinglocation,members_exams_proctorname,members_exams_application,members_exams_payment,members_exams_diploma,members_exams_processing")] members_exams members_exams)
{
if (ModelState.IsValid)
{
db.members_exams.Add(members_exams);
db.SaveChanges();
return RedirectToAction("Index");
}
List<SelectListItem> schoolsId = new List<SelectListItem>();
List<school> sch = db.schools.ToList();
sch.ForEach(x =>
{
schoolsId.Add(new SelectListItem { Text = x.schools_name, Value = x.schools_id.ToString() });
});
;
ViewBag.schools_id = schoolsId;
ViewBag.exams_id = new SelectList(db.exams, "exams_id", "exams_description", members_exams.exams_id);
ViewBag.members_exams_types_id = new SelectList(db.members_exams_types, "members_exams_types_id", "members_exams_types_description", members_exams.members_exams_types_id);
ViewBag.members_exams_status_id = new SelectList(db.members_exams_status, "members_exams_status_id", "members_exams_status_description", members_exams.members_exams_status_id);
return View(members_exams);
}
指數
<div class="form-group">
@Html.LabelFor(model => model.schools_id, "schools_id", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("schools_id", null, "---Select School---", htmlAttributes: new { @class = "form-control", @id = "ddlSchool" })
@Html.ValidationMessageFor(model => model.schools_id, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.members_exams_proctorname, "Proctor Name", htmlAttributes: new { @class = "control-label col-md-2" })
<div id="Name" class="col-md-10">
@Html.DropDownListFor(x => x.members_exams_proctorname, new List<SelectListItem>(), "---Select Name---", new { @id = "ddlName" })
@Html.ValidationMessageFor(model => model.members_exams_proctorname, "", new { @class = "text-danger" })
</div>
</div>
原索引
<div class="form-group">
@Html.LabelFor(model => model.members_exams_proctorname, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.members_exams_proctorname, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.members_exams_proctorname, "", new { @class = "text-danger" })
</div>
</div>
這將工作,如果我輸入的管理員名稱,但我正在尋找級聯下拉,將保存數據庫並綁定到模型。我試圖重做這幾天,但一直停留在「一個或多個實體的驗證失敗。有關更多詳細信息,請參閱'EntityValidationErrors'屬性。任何幫助表示讚賞。
建議你在[本DotNetFiddle]研究代碼(https://dotnetfiddle.net/1bPZym)。擺脫所有對ViewBag的使用並使用視圖模型。 –