2016-08-30 87 views
-1

我添加了一個下拉列表,以便在爲學校創建新的考試時與學校的聯繫人匹配該學校的一名學生。問題是當我試圖發佈我得到的錯誤。傳入字典的模型項目類型爲'x',但此字典需要'x'類型的模型項目

傳入字典的模型項目類型爲'Data.members_exams',但此字典需要類型爲'NAHPapp.Models.DropDownList'的模型項目。

我知道我應該從錯誤讀取的不同模型中傳遞,但是我不能指出在哪裏更改。我們的目標是能夠保存這篇文章。

控制器:

public ActionResult DropDownListCreate() 
    { 

     List<SelectListItem> schoolsId = new List<SelectListItem>(); 
     DropDownList DropDownList = new DropDownList(); 

     List<school> sch = db.schools.OrderBy(x => x.schools_name).ToList(); 
     sch.ForEach(x => 
     { 
      schoolsId.Add(new SelectListItem { Text = x.schools_name, Value = x.schools_id.ToString() }); 
     }); 
     DropDownList.schools_id = schoolsId; 


     ViewBag.schools_contacts = new SelectList(db.schools, "schools_contacts_id", "schools_contact_firstname"); 

     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"); 
     ViewBag.schools_id = new SelectList(db.schools.OrderBy(x => x.schools_name), "schools_id", "schools_name"); 


     return View(DropDownList); 
    } 


    public ActionResult Contact(string schools_id) 
    { 
     int schoolID; 
     List<SelectListItem> schoolsName = new List<SelectListItem>(); 
     if (!string.IsNullOrEmpty(schools_id)) 
     { 
      schoolID = Convert.ToInt32(schools_id); 
      List<schools_contacts> Name = db.schools_contacts.Where(x => x.schools_id == schoolID).ToList(); 
      Name.ForEach(x => 
      { 
       schoolsName.Add(new SelectListItem { Text = x.schools_contacts_firstname + " " + x.schools_contacts_lastname + " | " + x.schools_contacts_email, Value = x.schools_contacts_id.ToString() }); 
      }); 
     } 
     return Json(schoolsName, JsonRequestBehavior.AllowGet); 
    } 

    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult DropDownListCreate([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_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("Details", "members", new { id = members_exams.members_id }); //custom route to go to details of student. 
     } 

     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, "schools_id", "schools_name", members_exams.schools_id); 


     return View(members_exams); 
    } 

查看:

@using Data; 
@model NAHPapp.Models.DropDownList 

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 

<div class="form-group"> 
     @Html.LabelFor(model => model.schools_id, "School Name", htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.DropDownListFor(x => x.schools_id, Model.schools_id, "----Select----", new { @id = "ddlSchool" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.schools_id, "Proctor Name", htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div id="Name" class="col-md-10"> 
      @Html.DropDownListFor(x => x.schools_contacts_firstname, new List<SelectListItem>(), "--Select--", new { @id = "ddlName" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     @Html.LabelFor(model => model.exams_id, "Exams Id", htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-5"> 
      @Html.DropDownList("exams_id", null, htmlAttributes: new { @class = "form-control" }) 
      @Html.ValidationMessageFor(model => model.exams_id, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     <div class="col-md-offset-2 col-md-10"> 
      <input type="submit" value="Create" class="btn btn-success" /> | 
      @Html.ActionLink("Member Index", "Index", "members", null, new { @class = "btn btn-default" }) 
     </div> 
    </div> 
</div> 
} 
<script type="text/javascript"> 
$(document).ready(function() { 
    $('#ddlSchool').change(function() { 
     $.ajax({ 
      type: "post", 
      url: "/DropDownList/GetContact", 
      data: { schools_id: $('#ddlSchool').val() }, 
      datatype: "json", 
      traditional: true, 
      success: function (data) { 
       var Name = "<select id='ddlName'>"; 
       Name = Name + '<option value="">--Select--</option>'; 
       for (var i = 0; i < data.length; i++) { 
        Name = Name + '<option value=' + data[i].Value + '>' + data[i].Text + '</option>'; 
       } 
       Name = Name + '</select>'; 
       $('#Name').html(Name); 
      } 
     }); 
    }); 
}); 

任何幫助或指導將是非常讚賞。

回答

0

請在您的HttpPost操作方法中查看此行。

返回查看(members_exams)

在你HttpPost DropDownListCreate操作方法,如果模型狀態是無效的,你正在返回members_exams類的對象。但是您的觀點強烈類型爲DropDownList類型。 (在您的GET操作中,您發送的是正確的對象

您應該確保您返回的是相同類型的對象(就像您在GET操作中那樣)。

所以更換

return View(members_exams); 

隨着下面的代碼(我複製它從你的GET操作方法

DropDownList DropDownList = new DropDownList(); 
List<school> sch = db.schools.OrderBy(x => x.schools_name).ToList(); 
sch.ForEach(x => 
    { 
     schoolsId.Add(new SelectListItem { Text = x.schools_name, Value = x.schools_id.ToString() }); 
    }); 
DropDownList.schools_id = schoolsId; 
return View(DropDownList); 

雖然這將解決關於非匹配類型的當前的錯誤,您的看法將像您的初始GET視圖一樣顯示!我建議你花一點時間創建一個共同視圖模型,您可以從GET操作和POST操作發送到您的視圖(當ModelState.IsValidfalse時)

相關問題