2015-09-01 46 views
1

我設法使用ASP.NET MVC 5中的數據庫中的值填充DropDownList。我的目標是將dropDownList的值中的一個賦值給特定模型,並將其發送回數據庫。因此,如果我在下拉列表中保留默認值,則SQL服務器中的數據爲空,這是可以的,但如果我選擇一個選項,則會出現以下錯誤: 拋出異常:System.Web中出現'System.InvalidOperationException'。 Mvc.dll(「沒有類型爲」IEnumerable「的ViewData項具有」狀態「關鍵字。」)。到目前爲止,我嘗試了一切,我打開了一些建議。謝謝 !!! 在控制器:不尋常的行爲DropDownList MVC 5

ViewBag.Status = new SelectList(db.Status, "Id", "Name"); 

在查看

@Html.DropDownList("Status","Select status...") 

在控制器至今..

public ActionResult Index() 
     { 
      return View(); 
     } 
     [HttpGet] 
     public ActionResult Apply(ViewModelVM vm,int x=0) 
     { 
      myDb db = new myDb(); 
      ViewBag.SocialStatus = new SelectList(db.SocialStatuses, "Id", "StatusDescription"); 
      return View(); 
     } 
     [HttpPost] 
     public ActionResult Apply(ViewModelVM vm) 
     { 
      if (ModelState.IsValid) 
      { 
       using (myDb db = new myDb()) 
       { 
        var personalinfo = new PersonalInformation() 
        { 
         FirstName = vm.PersonalInformation.FirstName, 
         LastName = vm.PersonalInformation.LastName, 
         Birthdate = vm.PersonalInformation.Birthdate, 
         SocialStatus = vm.SocialStatus 
        }; 
        ViewBag.SocialStatus = new SelectList(db.SocialStatuses, "Id", "StatusDescription"); 
        db.PersonalInformations.Add(personalinfo); 
        db.SaveChanges(); 
       } 
       return View("Success"); 
       } 

      return View(); 
     } 

型號:

public partial class Status 
    { 
     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] 
     public SocialStatus() 
     { 
      PersonalInformations = new HashSet<PersonalInformation>(); 
     } 

     public int Id { get; set; } 

     [StringLength(20)] 
     public string StatusDescription { get; set; } 

     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
     public virtual ICollection<PersonalInformation> PersonalInformations { get; set; } 
    } 
} 

視圖模型:

public class ViewModelVM 
    { 
     ... 
     public Status SocialStatus { set; get; } 
     ... 
    } 
+0

因爲'Status'的值是'null'(可能是因爲你從POST動作返回視圖)。 –

+0

是的,我願意,但如果是這樣的話,這個問題的解決方案是什麼?我在這個問題上困擾了很多年......我將非常感謝您的幫助:) – FarCry88

+0

您的模型中有一個名爲'Status'的屬性嗎?當前如何在GET方法中填充集合? (你需要顯示相關的代碼) –

回答

2

首先您使用視圖模型,包括對SelectList

public IEnumerable<SelectListItem> StatusList { get; set; } 

下一頁在您的視圖模型中的財產刪除參數從GET方法的模型(因爲你不出現使用的x的價值,應該還刪除)

[HttpGet] 
public ActionResult Apply(ViewModelVM vm,int x=0) 
{ 
    myDb db = new myDb(); 
    ViewModelVM model = new ViewModelVM() 
    { 
    StatusList = new SelectList(db.SocialStatuses, "Id", "StatusDescription"); 
    }; 
    return View(model); // return the model to the view 
} 

接下來,你的下拉列表綁定到一個名爲Status屬性,但您的視圖模型不包含名爲status(SocialStatus)和SocialStatus的屬性是一個複雜的對象,並且不能將<select>綁定到複雜對象(<select>僅回發單個值(或<select multiple>的情況下的數組或值)。

此外,由於您的視圖模型包含屬性,該屬性是其屬性上帶有驗證屬性的複雜對象,因此ModelState將始終無效,因爲您不回發StatusDescription的值。因此,您總是返回POST方法中的視圖,並且因爲您沒有重新分配ViewBag.Status = ....,所以它是null,因此是錯誤。

刪除屬性public Status SocialStatus { set; get; },包括

[Display(Name = "Social Status")] 
[Required(ErrorMessage = "Please select a status")] 
public int SocialStatus { get; set; } 

的然後在視圖中,強烈地使用

@Html.LabelFor(m => m.SocialStatus) 
@Html.DropDownListFor(m => m.SocialStatus, Model.StatusList, "-Please select-") 
@Html.ValidationMessageFor(m => m.SocialStatus) 

綁定到你的模型。然後,在POST方法,如果ModelState是無效的,填充選擇在返回視圖之前再次列出

if(!ModelState.IsValid) 
{ 
    model.StatusList = new SelectList(db.SocialStatuses, "Id", "StatusDescription"); 
    return View(model); 
} 
// save and redirect 

最後,查看What is ViewModel in MVC?

+0

無限感謝..現在它工作正常。驚人。 – FarCry88