2015-02-11 92 views
-1

您好我是mvc4的新來者c#開發我有一個問題從下拉列表傳遞數據到我的數據庫,它一直給我一個錯誤,'我必須檢查以確定對象是null在調用方法之前'我真的很感謝一些幫助你的術語,因爲我一直試圖解決這個簡單問題兩天,現在沒有成功,這裏是我的代碼。從下拉列表到數據庫的數據c#mvc

CSHTML: 
    @model UnityServiceProject.Models.NewAccountModel 
    @{ 
     ViewBag.Title = "NewAccount"; 
    } 

    <h2>NewAccount</h2> 

    @using (@Html.BeginForm()){ 
    @Html.ValidationSummary(true,"Please fill all required fields Thank You.") 
    <fieldset> 
     <legend>Create Account</legend> 
     <ol> 

      <li>@Html.LabelFor(u => u.charityName)</li> 
      <li>@Html.TextBoxFor(u => u.charityName)</li> 

      <li>@Html.LabelFor(u => u.charityNumber)</li> 
      <li>@Html.TextBoxFor(u => u.charityNumber)</li> 

      <li>@Html.LabelFor(u => u.addressLine1)</li> 
      <li>@Html.TextBoxFor(u => u.addressLine1)</li> 

      <li>@Html.LabelFor(u => u.addressLine2)</li> 
      <li>@Html.TextBoxFor(u => u.addressLine2)</li> 

      <li>@Html.LabelFor(u => u.City)</li> 
      <li>@Html.TextBoxFor(u => u.City)</li> 

      <li>@Html.LabelFor(u => u.County)</li> 
      <li>@Html.DropDownList("Counties", Model.County)</li> 
      <li>@Html.LabelFor(u => u.Phone)</li> 
      <li>@Html.TextBoxFor(u => u.Phone)</li> 
      <li>@Html.LabelFor(u => u.Email)</li> 
      <li>@Html.TextBoxFor(u =>u.Email)</li> 
      <li>@Html.LabelFor(u => u.registeredPassword)</li> 
      <li>@Html.LabelFor(u => u.Comments)</li> 
      <li>@Html.TextBoxFor(u => u.Comments)</li> 
     </ol> 
    </fieldset> 
     <input type="submit" value="New Account" /> 
    } 

    MY MODEL: 
    using System; 
    using System.Collections.Generic; 
    using System.ComponentModel.DataAnnotations; 
    using System.Linq; 
    using System.Web; 
    using System.Web.Mvc; 
    using UnityServiceProject.Models; 


    namespace UnityServiceProject.Models 
    { 
     public class NewAccountModel 
     { 
      [Required] 
      [StringLength(150)] 
      [Display(Name="Charity Name: ")] 
      public string charityName { get; set; } 

      [Required] 
      [StringLength(150)] 
      [Display(Name="Charity Number: ")] 
      public string charityNumber { get; set; } 

      [Required] 
      [StringLength(100)] 
      [Display(Name="Address Line 1: ")] 
      public string addressLine1 { get; set; } 

      [Required] 
      [StringLength(100)] 
      [Display(Name="Address Line 2: ")] 
      public string addressLine2 { get; set; } 

      [Required] 
      [StringLength(100)] 
      [Display(Name="City: ")] 
      public string City { get; set; } 



      [Required] 
      [StringLength(100)] 
      [Display(Name = "Select County: ")] 
      public SelectList County { get; set; } 

      [Required] 
      [StringLength(100)] 
      [Display(Name="Phone Number: ")] 
      public string Phone { get; set; } 

      [Required] 
      [EmailAddress] 
      [StringLength(150)]//'UserLogin table field' setting max amount of characters 
      [Display(Name = "Registered Email Address: ")] 
      public string Email { get; set; } 

      [Required] 
      [DataType(DataType.Password)] 
      [StringLength(200)] 
      [Display(Name = "Registered Password: ")] 
      public string registeredPassword { get; set; } 



      [StringLength(300)] 
      [Display(Name = "Comments: ")] 
      public string Comments { get; set; } 
     } 
    } 

    MY CONTROLLER 
    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Web; 
    using System.Web.Mvc; 
    using System.Configuration; 
    using UnityServiceProject.Models; 


    namespace UnityServiceProject.Controllers 
    { 
     public class NewAccountController : Controller 
     { 
      // 
      // GET: /NewAccount/ 

      public ActionResult Index() 
      { 
       return View(); 
      } 

      [HttpGet] 
      public ActionResult NewAccount() 
      { 
       return View(); 
      } 

      [HttpPost] 
      public ActionResult NewAccount(NewAccountModel newAcc) 
      { 
       List<SelectListItem> listItem = new List<SelectListItem>(); 
       NewAccountModel nam = new NewAccountModel(); 





      listItem.Add(new SelectListItem() { Value = "1", Text = "Antrim" } 
     ); 
      listItem.Add(new SelectListItem() { Value = "2", Text = "Armagh" } 
     ); 
      listItem.Add(new SelectListItem() { Value = "3", Text = "Carlow" } 
     ); 
      listItem.Add(new SelectListItem() { Value = "4", Text = "Cavan" } 
     ); 
      listItem.Add(new SelectListItem() { Value = "5", Text = "Clare" } 
     ); 
      listItem.Add(new SelectListItem() { Value = "6", Text = "Cork" } 
     ); 
      listItem.Add(new SelectListItem() { Value = "7", Text = "Derry" } 
     ); 
      listItem.Add(new SelectListItem() { Value = "8", Text = "Donegal" } 
    ); 
      listItem.Add(new SelectListItem() { Value = "9", Text = "Down" } 
    ); 
      listItem.Add(new SelectListItem() { Value = "10", Text = "Dublin" } 
    ); 
     listItem.Add(new SelectListItem() { Value = "11", Text ="Fermanagh"} 
    ); 
     listItem.Add(new SelectListItem() { Value = "12", Text = "Galway" } 
    ); 
     listItem.Add(new SelectListItem() { Value = "13", Text = "Kerry" } 
    ); 
     listItem.Add(new SelectListItem() { Value = "14", Text = "Kildare" } 
    ); 
     listItem.Add(new SelectListItem() { Value = "15", Text = "Kilkenny"} 
    ); 
     listItem.Add(new SelectListItem() { Value = "16", Text = "Laois" } 
    ); 
     listItem.Add(new SelectListItem() { Value = "17", Text = "Leitrim" } 
    ); 
     listItem.Add(new SelectListItem() { Value = "18", Text = "Limerick"} 
    ); 
     listItem.Add(new SelectListItem() { Value = "19", Text "Longford" } 
    ); 
     listItem.Add(new SelectListItem() { Value = "20", Text = "Louth" } 
    ); 
     listItem.Add(new SelectListItem() { Value = "21", Text = "Mayo" } 
    ); 
     listItem.Add(new SelectListItem() { Value = "22", Text = "Meath" } 
    ); 
     listItem.Add(new SelectListItem() { Value = "23", Text = "Monaghan"} 
    ); 
     listItem.Add(new SelectListItem() { Value = "24", Text = "Offaly" } 
    ); 
     listItem.Add(new SelectListItem() { Value = "25", Text "Roscommon" } 
    ); 
     listItem.Add(new SelectListItem() { Value = "26", Text = "Sligo" } 
    ); 
     listItem.Add(new SelectListItem() { Value = "27", Text ="Tipperary"} 
     ); 
     listItem.Add(new SelectListItem() { Value = "28", Text = "Tyrone" } 
    ); 
     listItem.Add(new SelectListItem() { Value = "4", Text = "Waterford"} 
    ); 
     listItem.Add(new SelectListItem() { Value = "4", Text "Westmeath" } 
    ); 
     listItem.Add(new SelectListItem() { Value = "4", Text = "Wexford" } 
    ); 
     listItem.Add(new SelectListItem() { Value = "4", Text = "Wicklow" } 
    ); 


     nam.County = new SelectList(listItem, "Value", "Text"); 

     //if (listItem != null) 
     //{ 
     // listItem.Clear(); 
     //} 


      if(ModelState.IsValid) 
      { 
       using (var db = new UnityServiceEntities()) 
       { 
        //create New Account entity 
        var adNewAcc = db.AddNewAccounts.Create(); 

        adNewAcc.charityName = newAcc.charityName; 
        adNewAcc.charityNumber = newAcc.charityNumber; 
        adNewAcc.addressLine1 = newAcc.addressLine1; 
        adNewAcc.addressLine2 = newAcc.addressLine2; 
        adNewAcc.City = newAcc.City; 
        adNewAcc.County = newAcc.County.SelectedValue.ToString(); 
        adNewAcc.Phone = newAcc.Phone; 
        adNewAcc.emailAddress = newAcc.Email; 
        adNewAcc.registeredPassword = newAcc.registeredPassword; 
        adNewAcc.Comments = newAcc.Comments; 

        db.AddNewAccounts.Add(adNewAcc); 
        db.SaveChanges(); 
        return RedirectToAction("Index", "Home"); 
       } 
      } 
      return View(newAcc); 
     } 

    } 
    } 
Thank You! 
+0

編輯您的問題以刪除所有不相關的代碼。沒有人想通過所有這些 - 尤其是添加'SelectListItem'的數百行。您的問題涉及您的模型中的一個屬性 - 顯示與此相關的代碼。並顯示你得到的實際錯誤信息。 – 2015-02-12 02:58:59

回答

0

你必須填寫您的下拉列表中獲取方法

你必須創建這個方法裏面的模型,並設置國家 值下拉列表

[HttpGet] 
       public ActionResult NewAccount() 
       { 
        NewAccountModel accModel =new NewAccountModel(); 
        List<SelectListItem> listItem = new List<SelectListItem>(); 
listItem.Add(new SelectListItem() { Value = "1", Text = "Antrim" } 
     ); 
      listItem.Add(new SelectListItem() { Value = "2", Text = "Armagh" } 
     ); 
      listItem.Add(new SelectListItem() { Value = "3", Text = "Carlow" } 
     ); 
      listItem.Add(new SelectListItem() { Value = "4", Text = "Cavan" } 
     ); 
      listItem.Add(new SelectListItem() { Value = "5", Text = "Clare" } 
     ); 
      listItem.Add(new SelectListItem() { Value = "6", Text = "Cork" } 
     ); 
      listItem.Add(new SelectListItem() { Value = "7", Text = "Derry" } 
     ); 
      listItem.Add(new SelectListItem() { Value = "8", Text = "Donegal" } 
    ); 
      listItem.Add(new SelectListItem() { Value = "9", Text = "Down" } 
    ); 
      listItem.Add(new SelectListItem() { Value = "10", Text = "Dublin" } 
    ); 
     listItem.Add(new SelectListItem() { Value = "11", Text ="Fermanagh"} 
    ); 
     listItem.Add(new SelectListItem() { Value = "12", Text = "Galway" } 
    ); 
     listItem.Add(new SelectListItem() { Value = "13", Text = "Kerry" } 
    ); 
     listItem.Add(new SelectListItem() { Value = "14", Text = "Kildare" } 
    ); 
     listItem.Add(new SelectListItem() { Value = "15", Text = "Kilkenny"} 
    ); 
     listItem.Add(new SelectListItem() { Value = "16", Text = "Laois" } 
    ); 
     listItem.Add(new SelectListItem() { Value = "17", Text = "Leitrim" } 
    ); 
     listItem.Add(new SelectListItem() { Value = "18", Text = "Limerick"} 
    ); 
     listItem.Add(new SelectListItem() { Value = "19", Text "Longford" } 
    ); 
     listItem.Add(new SelectListItem() { Value = "20", Text = "Louth" } 
    ); 
     listItem.Add(new SelectListItem() { Value = "21", Text = "Mayo" } 
    ); 
     listItem.Add(new SelectListItem() { Value = "22", Text = "Meath" } 
    ); 
     listItem.Add(new SelectListItem() { Value = "23", Text = "Monaghan"} 
    ); 
     listItem.Add(new SelectListItem() { Value = "24", Text = "Offaly" } 
    ); 
     listItem.Add(new SelectListItem() { Value = "25", Text "Roscommon" } 
    ); 
     listItem.Add(new SelectListItem() { Value = "26", Text = "Sligo" } 
    ); 
     listItem.Add(new SelectListItem() { Value = "27", Text ="Tipperary"} 
     ); 
     listItem.Add(new SelectListItem() { Value = "28", Text = "Tyrone" } 
    ); 
     listItem.Add(new SelectListItem() { Value = "4", Text = "Waterford"} 
    ); 
     listItem.Add(new SelectListItem() { Value = "4", Text "Westmeath" } 
    ); 
     listItem.Add(new SelectListItem() { Value = "4", Text = "Wexford" } 
    ); 
     listItem.Add(new SelectListItem() { Value = "4", Text = "Wicklow" } 
    ); 


     accModel.County = new SelectList(listItem, "Value", "Text"); 
        return View(accModel); 
       } 

並在實體中創建國家/地區ID

而在HTML中你必須編寫下拉這樣

@Html.DropDownListFor(model => model.CountryID, Model.Country, "Select Country", new {id = "ddlCountry"}) 

希望這有助於。

相關問題