2013-08-21 46 views
5

我正在使用ASP.NET MVC4,現在我想添加一個下拉列表與我的mysql數據庫中的數據。這是我做的:值不能爲空。參數名稱:項目(DrodownList)

在我視圖(Register.cshtml):`

<div class="control-group"> 
    @Html.LabelFor(m => m.DistrictID, new { @class= "control-label"}) 
    <div class="controls"> 
     @Html.DropDownListFor(model => model.DistrictID, new SelectList(ViewBag.Districts, "district_id", "district_name", Model.DistrictID)) 
    </div> 
</div> 

在我控制器(的AccountController):

[AllowAnonymous] 
public ActionResult Register() 
{ 
    var districts = repository.GetDistricts(); 
    ViewBag.Districts = districts; 

    return View(new RegisterModel()); 
} 

// 
// POST: /Account/Register 

[HttpPost] 
[AllowAnonymous] 
[ValidateAntiForgeryToken] 
public ActionResult Register(RegisterModel model) 
{ 

    if (ModelState.IsValid) 
    { 
     // Attempt to register the User 
     try 
     { 
      MembershipService.CreateUser(model.Name, model.FamilyName, model.BirthDate, model.Sex, model.Nationality, model.Email, model.UserName, model.Password, model.Street, model.StreetNr); 

      FormsAuthentication.SetAuthCookie(model.UserName, false); 
      return RedirectToAction("Index", "Home"); 
     } 
     catch (ArgumentException ae) 
     { 
      ModelState.AddModelError("", ae.Message); 
     } 
    } 

    // If we got this far, something failed, redisplay form 
    return View(model); 
} 

我從區我的存儲庫是這樣的:

public IQueryable<district> GetDistricts() 
{ 
    return from district in entities.districts 
      orderby district.district_name 
      select district; 
} 

我RegisterModel:

public class RegisterModel 
{ 
    [Required] 
    [Display(Name = "Given name")] 
    public string Name { get; set; } 

    [Required] 
    [Display(Name = "Family name")] 
    public string FamilyName { get; set; } 

    [Required] 
    [Display(Name = "Birthdate")] 
    public DateTime BirthDate { get; set; } 

    [Required] 
    [Display(Name = "Sex")] 
    public string Sex { get; set; } 

    [Required] 
    [Display(Name = "Nationality")] 
    public string Nationality { get; set; } 

    [Required] 
    [Display(Name = "Email")] 
    public string Email { get; set; } 

    [Required] 
    [Display(Name = "User name")] 
    public string UserName { get; set; } 

    [Required] 
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)] 
    [DataType(DataType.Password)] 
    [Display(Name = "Password")] 
    public string Password { get; set; } 

    [DataType(DataType.Password)] 
    [Display(Name = "Confirm password")] 
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] 
    public string ConfirmPassword { get; set; } 

    [Required] 
    [Display(Name = "Street")] 
    public string Street { get; set; } 

    [Required] 
    [Display(Name = "Street Number")] 
    public int StreetNr { get; set; } 

    [Required] 
    [Display(Name = "District")] 
    public IEnumerable<SelectListItem> Districts { get; set; } 

    public int DistrictID { get; set; } 
} 

的下拉列表填充區,但是當我點擊「註冊」我得到這個錯誤:

Value cannot be null. Parameter name: items

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentNullException: Value cannot be null. Parameter name: items

當我調試的方法我看到的ModelState是無效。
密鑰11是DistrictID,包含密碼但密鑰12是區域並給出錯誤:"The district field is required" ...

我在做什麼錯了?

+0

僅當驗證失敗或者驗證通過時才引發異常? – Andrei

回答

6

考慮模型驗證失敗的情況。查看將再次與隨請求一起發送的模型重新顯示。但是這條線:

new SelectList(ViewBag.Districts, "district_id", "district_name", Model.Districts) 

將有null作爲第一個參數,因爲ViewBag.Districts沒有重新填充,導致異常。因此,爲了避免異常,只需再次設置此屬性:

// If we got this far, something failed, redisplay form 
var districts = repository.GetDistricts(); 
ViewBag.Districts = districts; 
return View(model); 

更新。當看到模型定義時,立即進入頭腦的東西就是Districts集合的Required屬性。您很可能不需要用戶輸入這些內容,也不需要將它們保存到數據庫中。嘗試刪除此屬性,並且關於此屬性的錯誤將消失。

+0

現在,當我點擊「註冊」,它只是重新加載頁面,值仍然填充,但沒有任何反應......也沒有將其添加到數據庫 – nielsv

+0

@ niels123,你有沒有嘗試過調試?至少你可以做的是爲'AddModelError'使用一些有意義的鍵而不是空字符串,並用Html.ValidationMessage將它顯示在頁面的某個地方 - 這將顯示是否發生異常。但是,理解錯誤的最好方法就是調試該方法。 – Andrei

+0

我檢查了方法,並且modelstate無效。關鍵11(DistrictID)具有我的特點,但重點12(區)給出錯誤:「區域是必需的」。 – nielsv

相關問題