2017-02-14 62 views
0

尋找一些幫助將AJAX值(members_exams_proctorname)綁定到模型中,以便在發佈期間保存。學校放下工作,並將獲得監考人名字的價值。當我檢查元素時,我可以看到監督員的姓名。Casecade DropDownList w/Ajax綁定到模型

Inspect Element

型號

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'屬性。任何幫助表示讚賞。

+1

建議你在[本DotNetFiddle]研究代碼(https://dotnetfiddle.net/1bPZym)。擺脫所有對ViewBag的使用並使用視圖模型。 –

回答

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> ") 
       }); 
     }) 
    }); 

});