寻找有关将AJAX值(members_exams_proctorname)绑定到模型中的一些帮助,以便在发布期间保存。学校放下工作,将获得监考员姓名的价值。当我检查元素时,我可以看到监考人的名字。
模型
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>
如果我输入proctor名称,这将有效,但我正在寻找将保存数据库并绑定到模型的级联下拉列表。我已经尝试过几天重做这件事,但一直停留在&#34;一个或多个实体的验证失败。请参阅&#39; EntityValidationErrors&#39;物业详情及#34;。任何帮助表示赞赏。
答案 0 :(得分:0)
public ActionResult CreateForSchool(int? id, string name, string mname, string lname, string ssn, string email, string address, string city, string state, string zip, string phone, string username, DateTime expirationDate, short? schools_id, string members_exams_proctorname, string members_exams_testinglocation)
{
members_exams members_exams = new members_exams();
string randoms = Guid.NewGuid().ToString().Replace("-", string.Empty).Replace("+", string.Empty).Substring(0, 4).ToUpper();
members_exams.members_id = id;
members_exams.members_exams_firstname = name;
members_exams.members_exams_middlename = mname;
members_exams.members_exams_lastname = lname;
members_exams.members_exams_ssn = ssn;
members_exams.members_exams_email = email;
members_exams.members_exams_address = address;
members_exams.members_exams_city = city;
members_exams.members_exams_state = state;
members_exams.members_exams_zip = zip;
members_exams.members_exams_phone = phone;
members_exams.members_exams_expirationdate = expirationDate;
members_exams.members_exams_username = name.Substring(0, Math.Min(name.Length, 1)) + lname.Substring(0, Math.Min(lname.Length, 3)) + ssn.Substring(5, Math.Min(ssn.Length, 4)) + randoms;
members_exams.schools_id = schools_id;
members_exams.members_exams_proctorname = members_exams_proctorname;
members_exams.members_exams_testinglocation = members_exams_testinglocation;
var exam = (from x in db.members_exams
join y in db.schools_contacts on x.schools_id equals y.schools_id
join z in db.schools on x.schools_id equals z.schools_id
where x.schools_id == schools_id
select x).ToList();
ViewBag.SchoolName = new SelectList(db.schools.Where(x => x.schools_active == true).OrderBy(x => x.schools_name), "schools_id", "schools_name");
ViewBag.Proctor = new SelectList(from x in exam.ToList()
select new
{
members_exams_proctorname = x.members_exams_proctorname
}, "schools_contacts_firstname", "schools_contacts_firstname");
ViewBag.Location = new SelectList(from x in exam.ToList()
select new
{
embers_exams_testinglocation = x.members_exams_testinglocation
}, "schools_id", "schools_name");
ViewBag.exams_id = new SelectList(db.exams, "exams_id", "exams_description");
ViewBag.members_exams_status_id = new SelectList(db.members_exams_status, "members_exams_status_id", "members_exams_status_description");
ViewBag.members_exams_types_id = new SelectList(db.members_exams_types, "members_exams_types_id", "members_exams_types_description");
return View(members_exams);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateForSchool([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("CreateForSchool", "Members");
}
ViewBag.members_exams_proctorname = new SelectList((from s in db.schools_contacts.OrderBy(s => s.school.schools_name).ToList()
select new
{
schools_contacts_id = s.schools_contacts_id,
FullString = s.school.schools_name + " " + s.schools_contacts_firstname + " " + s.schools_contacts_lastname
}),
"schools_contacts_id",
"FullString",
null);
ViewBag.exams_id = new SelectList(db.exams, "exams_id", "exams_description", members_exams.exams_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);
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.schools_id = new SelectList(db.schools.Where(x => x.schools_active == true).OrderBy(s => s.schools_name), "schools_id", "schools_name");
return View(members_exams);
}
查看
<div class="form-group">
@Html.LabelFor(model => model.schools_id, "School", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(x => x.schools_id, ViewBag.SchoolName as SelectList, "--- Select School ---", new { @class = "form-control", id = "SchID" })
@Html.ValidationMessageFor(model => model.schools_id, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.members_exams_proctorname, "Proctor", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(x => x.members_exams_proctorname, ViewBag.Proctor as SelectList, new { @class = "form-control", id = "Proctor" })
@Html.ValidationMessageFor(model => model.members_exams_proctorname, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.members_exams_testinglocation, "Exam Location", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(x => x.members_exams_testinglocation, ViewBag.Location as SelectList, new { @class = "form-control", id = "Location" })
@Html.ValidationMessageFor(model => model.members_exams_testinglocation, "", new { @class = "text-danger" })
</div>
</div>
脚本
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script>
$(function () {
$("#SchID").change(function () {
$.get("http://localhost/NAHPapp/members_exams/GetProctorbyId", { schools_id: $("#SchID").val() }, function (data) {
$("#Proctor").empty();
$.each(data,
function (index, row) {
$("#Proctor").append(" <option value=' " + row.schools_contacts_firstname + ' ' + row.schools_contacts_lastname + " '>" + row.schools_contacts_firstname + ' ' + row.schools_contacts_lastname + " </option> ")
});
})
});
$("#SchID").change(function () {
$.get("http://localhost/NAHPapp/members_exams/GetSchoolLocation", { schools_id: $("#SchID").val() }, function (data) {
$("#Location").empty();
$.each(data,
function (index, row) {
$("#Location").append(" <option value=' " + row.schools_name + " '>" + row.schools_name + " </option> ")
});
})
});
});