0
型號System.NullReferenceException:未將對象引用設置爲對象的實例。自舉下拉列表
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")
}
每次我看到這是因爲列表沒有初始化。查看get方法並確保所有列表都已初始化。 –
幾乎所有的'NullReferenceException'都是一樣的。請參閱「[什麼是.NET中的NullReferenceException?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)」的一些提示。 –