System.NullReferenceException:未将对象引用设置为对象的实例。 bootstrap下拉列表

时间:2013-10-11 16:43:15

标签: asp.net-mvc-4 twitter-bootstrap nullreferenceexception helper html.dropdownlistfor

模型

public class Employee
    {
        public int EmployeeID { get; set; }
        public string Title { get; set; }
        [Required(ErrorMessage = "Please enter first name.")]
        [StringLength(500)]
        public string FirstName { get; set; }
        public string MI { get; set; }
        [Required(ErrorMessage = "Please enter last name.")]
        [StringLength(500)]
        public string LastName { get; set; }
        public string Gender { get; set; }

        [Required(ErrorMessage = "Please select date of birth.")]
        public Nullable<System.DateTime> DateOfBirth { get; set; }

        [Required(ErrorMessage = "Please enter email.")]
        [StringLength(500)]
        [EmailAddress]
        public string Email { get; set; }

        [Required(ErrorMessage = "Please enter street.")]

        [StringLength(500)]
        public string Street { get; set; }

        [Required(ErrorMessage = "Please enter state.")]
        [StringLength(50)]
        public string State { get; set; }

        [Required(ErrorMessage = "Please enter city.")]
        [StringLength(50)]
        public string City { get; set; }

        [Required(ErrorMessage = "Please enter country.")]
        [StringLength(50)]
        public string Country { get; set; }

        [Required(ErrorMessage = "Please enter post code.")]
        [StringLength(6)]
        public string PostCode { get; set; }

        [Required(ErrorMessage = "Please enter home phone.")]
        public string HomePhone { get; set; }

        [Required(ErrorMessage = "Please enter mobile phone.")]

        public string MobilePhone { get; set; }


    }

控制器方法

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Employee employee)
{
    if (ModelState.IsValid)
    {
        db.Employees.Add(employee);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(employee);
}

查看

@model EmployeeManagementSystem.Models.Employee

@{
    ViewBag.Title = "Create";
}


<h2>Create</h2>

<div class="row-fluid">
    <div class="span12">
        @using (Html.Bootstrap().Begin(new Form().Id("CreateEmployee").Type(FormType.Inline)))
        {

            @Html.AntiForgeryToken()

            <div class="span12">

                <fieldset>
                    <div class="span4">

                        <legend>Employee Info</legend>

                        @Html.Bootstrap().ControlGroup().TextBoxFor(x => x.FirstName).Size(InputSize.BlockLevel).ShowValidationMessage(true, HelpTextStyle.Block)
                        @Html.Bootstrap().ControlGroup().TextBoxFor(x => x.LastName).Size(InputSize.BlockLevel).ShowValidationMessage(true, HelpTextStyle.Block)
                        @Html.Bootstrap().ControlGroup().DropDownListFor(m => m.Gender, ViewBag.GenderList as SelectList).Size(InputSize.BlockLevel).ShowValidationMessage(true, HelpTextStyle.Block)
                        @Html.Bootstrap().ControlGroup().Class("input-append date").TextBoxFor(x => x.DateOfBirth).Size(InputSize.BlockLevel).AppendIcon(Icons.calendar).ShowValidationMessage(true, HelpTextStyle.Block)
                        @Html.Bootstrap().ControlGroup().TextBoxFor(x => x.Email).Size(InputSize.BlockLevel).ShowValidationMessage(true, HelpTextStyle.Block)
                    </div>
                    <div class="span8 ">
                        <legend>Contact Details</legend>

                        @Html.Bootstrap().ControlGroup().TextBoxFor(x => x.Street).Size(InputSize.BlockLevel).ShowValidationMessage(true, HelpTextStyle.Block)

                        <div class="span6" style="margin-left:0px">
                            @Html.Bootstrap().ControlGroup().TextBoxFor(x => x.City).Size(InputSize.BlockLevel).ShowValidationMessage(true, HelpTextStyle.Block)
                            @Html.Bootstrap().ControlGroup().TextBoxFor(x => x.State).Size(InputSize.BlockLevel).ShowValidationMessage(true, HelpTextStyle.Block)
                            @Html.Bootstrap().ControlGroup().TextBoxFor(x => x.HomePhone).Size(InputSize.BlockLevel).ShowValidationMessage(true, HelpTextStyle.Block).HtmlAttributes(new Dictionary<string, Object> { { "data-mask", "(99999)(999999)" } })
                        </div>
                        <div class="span6">
                            @Html.Bootstrap().ControlGroup().TextBoxFor(x => x.PostCode).Size(InputSize.BlockLevel).ShowValidationMessage(true, HelpTextStyle.Block)
                            @Html.Bootstrap().ControlGroup().TextBoxFor(x => x.Country).Size(InputSize.BlockLevel).ShowValidationMessage(true, HelpTextStyle.Block)
                            @Html.Bootstrap().ControlGroup().TextBoxFor(x => x.MobilePhone).Size(InputSize.BlockLevel).ShowValidationMessage(true, HelpTextStyle.Block).HtmlAttributes(new Dictionary<string, Object> { { "data-mask", "+99 (9999)(999999)" } })


                        </div>
                    </div>
                </fieldset>

                @Html.Bootstrap().SubmitButton().Style(ButtonStyle.Primary)
                @Html.Bootstrap().ActionLinkButton("Cancel", "Index").Style(ButtonStyle.Default)


            </div>
        }
    </div>
</div>

@section Scripts {
    <script type="text/javascript">
        $(document).ready(function () {
            //$('[rel=tooltip]').tooltip();

            $('#CreateEmployee .input-append.date').datepicker({
                startView: 2,
                autoclose: true
            });

        });
    </script>

    @Scripts.Render("~/bundles/jqueryval")
    @Scripts.Render("~/bundles/BootstrapFormHelpersJS")
    @Styles.Render("~/Content/BootstrapFormHelpersCSS")
}

1 个答案:

答案 0 :(得分:0)

嗨,这是我控制器的get方法。

[HttpGet]
        public ActionResult Create()
        {
            List<string> genders = new List<string>();
            genders.Add("--Select--");
            genders.Add("Male");
            genders.Add("Female");
            genders.Add("Not Defind");
            ViewBag.GenderList = new SelectList(genders, "--Select--");
            return View();
        }