2014-02-28 110 views
0

當我通過單擊提交按鈕發佈我的AJAX表單時,出現500錯誤。該處理AJAX後控制器獲取數據正常,但當我返回局部視圖,通過這條線,我得到了500:DropDownListFor()和500錯誤

return PartialView("_SiteSurveyNewClubTeam", model); 

我返回部分回,而不是HTTP狀態的原因代碼是因爲如果我不這樣做,我的一個動態下拉菜單回來了。也許我正在把自己鎖在一個角落裏,在這裏。

在違規DropDownListFor()我相信提供的數據類型是否正確,並以正確的順序:(string, IList<SelectListItem>)

錯誤

The ViewData item that has the key 'DistrictSelected' is of type 'System.String' 
but must be of type 'IEnumerable<SelectListItem>'. 

視圖模型聲明

public IList<SelectListItem> DistrictSelect { get; set; } 
public string DistrictSelected { get; set; } 

來源o F中的錯誤是這條線在我看來

<span class="formColumn2">@Html.DropDownListFor(model => model.DistrictSelected, Model.DistrictSelect)</span> 

不知道爲什麼我收到此。有任何想法嗎?

感謝

這裏是處理AJAX表單提交

[HttpPost] 
public ActionResult ProcessFormANewClubTeam(FormANewClubTeamViewModel model) 
{ 
    var httpStatus = HttpStatusCode.BadRequest; 
    var cosponsors = new List<NewClubSponsor>(); 
    var errorMessages = new StringBuilder(); 
    var tasks = new NewClubBuilderTasks(); 
    var clubKeyNumber = tasks.GetClubKeyNumber(); 
    var masterCustomerId = tasks.GetMasterCustomerId(); 
    bool exceptionRaised = false; 

    if (ModelState.IsValid) 
    { 


     if (model.NewClub_Id > 0) 
     { 
       //Load the entity to be partially-updated 
       NewClub newClub = db.NewClubs.Single(nc => nc.Id == model.NewClub_Id); 

       //Set the values for the fields to be updated 
       newClub.District = model.DistrictSelected; 
       newClub.Division = model.DivisionSelected; 
       newClub.Region = Utility.Personify.GetRegionFromDistrict(newClub.District); 
       newClub.ClubCounselorMasterCustomerId = model.ClubCounselorMasterCustomerId; 
       newClub.ClubCounselorContact = model.ClubCounselorContact; 
       newClub.ClubCounselorEmail = model.ClubCounselorEmail; 
       newClub.ClubCounselorPhone = model.ClubCounselorPhone; 
       newClub.DateUpdated = DateTime.Now; 

       try 
       { 
        //Execute the UPDATE 
        var dbResult = db.SaveChanges() > 0; 
        httpStatus = HttpStatusCode.OK; 

       } 
       catch (SqlException ex) 
       { 
        //Catch exceptions here 
       } 

      // return new HttpStatusCodeResult((int) httpStatus); 
      return PartialView("_SiteSurveyNewClubTeam", model); 

    } else { 

      var errors = ModelState 
      .Where(x => x.Value.Errors.Count > 0) 
      .Select(x => new {x.Key, x.Value.Errors}) 
      .ToArray(); 

      return new HttpStatusCodeResult((int) httpStatus); 

    } 
} 
+0

你可以發佈ajax嗎? – ganders

+0

是的,AJAX表格確實發佈的很好 – Slinky

+1

對不起,我的意思是粘貼ajax代碼在這裏... – ganders

回答

1

你必須重新填充在行動後的DistrictSelect列表中選擇列表項的代碼。您發佈的視圖模型將DistrictSelect的值設置爲null,這就是爲什麼當您渲染部分內容時出現異常。

+0

哇的方法,謝謝你的理解。我在其他地方尋找 – Slinky