2015-02-11 89 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"}) 

希望这有助于。

相关问题