2013-08-21 8 views
5

मैं एएसपी.नेट एमवीसी 4 के साथ काम कर रहा हूं और अब मैं अपने mysql डेटाबेस से डेटा के साथ एक ड्रॉपडाउन सूची जोड़ना चाहता हूं।मूल्य शून्य नहीं हो सकता है। पैरामीटर नाम: आइटम (ड्रोडडाउनलिस्ट)

मेरी दृश्य (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; } 
} 

dropdownlist जिलों से भर जाता है लेकिन जब मैं पर क्लिक करें "रजिस्टर" मैं इस त्रुटि मिलती है:

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 जिलाइड है और जिलाइड शामिल है लेकिन कुंजी 12 जिलों है और त्रुटि देता है: "The district field is required" ...

मैं क्या गलत कर रहा हूं?

+0

अपवाद केवल तभी फेंक दिया जाता है जब सत्यापन विफल हो जाता है, या जब सत्यापन भी पास हो जाता है? – Andrei

उत्तर

6

मॉडल सत्यापन विफल होने पर मामले पर विचार करें। अनुरोध के साथ भेजे गए मॉडल के साथ दृश्य फिर से फिर से चलाया जाएगा। हालांकि इस लाइन:

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

null पहली बार एक पैरामीटर के रूप में होगा, के बाद से ViewBag.Districts repopulated नहीं किया गया था, अपवाद के कारण। तो क्रम अपवाद से बचने के लिए बस फिर यह गुण सेट:

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

अद्यतन। मॉडल परिभाषा को देखते समय, जो चीज तुरंत दिमाग में आती है RequiredDistricts संग्रह की विशेषता है। सबसे अधिक संभावना है कि आपको उपयोगकर्ता में से किसी एक को दर्ज करने की आवश्यकता नहीं है, न ही आप उन्हें डेटाबेस में सहेजते हैं। इस विशेषता को हटाने का प्रयास करें, और इस संपत्ति के बारे में त्रुटि गायब हो जाएगी।

+0

अब, जब मैं "रजिस्टर" पर क्लिक करता हूं तो यह पृष्ठ को फिर से लोड करता है और मान अभी भी भर जाते हैं लेकिन कुछ नहीं होता है ... इसे डेटाबेस – nielsv

+0

@ niels123 में भी नहीं जोड़ा गया है, क्या आपने डीबगिंग करने का प्रयास किया है? कम से कम आप क्या कर सकते हैं 'AddModelError' के लिए खाली स्ट्रिंग के बजाय कुछ सार्थक कुंजी का उपयोग करना और उसे HTML पर VtmlationMessage के साथ कहीं भी प्रदर्शित करना - यह दिखाएगा कि अपवाद होता है या नहीं। लेकिन फिर भी, समझने का सबसे अच्छा तरीका क्या गलत है विधि को डीबग करना है। – Andrei

+0

मैंने विधि की जांच की और मॉडलस्टेट मान्य नहीं है। कुंजी 11 (जिलाइड) में मेरे जिलाइड का मूल्य है लेकिन कुंजी 12 (जिलों) एक त्रुटि देता है: "जिला क्षेत्र आवश्यक है"। – nielsv

संबंधित मुद्दे

 संबंधित मुद्दे