从下拉列表到db c#mvc的数据

时间:2015-02-11 14:36:58

标签: asp.net-mvc-4

您好我是mvc4 c#开发的新来者我有一个问题从下拉列表将数据传递到我的数据库它一直给我一个错误'我必须检查以确定该对象在调用方法之前是否为空'我我真的很感谢有些帮助,因为我一直试图解决这个简单的问题两天,但现在没有成功,这是我的代码。

    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!

1 个答案:

答案 0 :(得分:0)

您必须在Get方法

中填写下拉列表
  

您必须在此方法中创建模型并设置国家/地区   下拉列表的值

    [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 );
                }
  

在实体

中创建countryID

在Html中你必须像这样写下拉列表

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

希望这有帮助。

相关问题