Validation 在下拉式MVC上验证失败

Validation 在下拉式MVC上验证失败,validation,ef-code-first,html.dropdownlistfor,asp.net-mvc-viewmodel,Validation,Ef Code First,Html.dropdownlistfor,Asp.net Mvc Viewmodel,我首先使用EF的代码。下拉列表上的验证似乎失败,错误为System.NullReferenceException:对象引用未设置为对象的实例。当我保存一条记录并故意将控件留空以测试验证时,就会发生这种情况。即使下拉列表本身有一个选择,也会发生这种情况 以下是我的部分观点: <div class="editor"> @Html.LabelFor(model => model.EmployeeID) @Html.DropDownListFor(model =>

我首先使用EF的代码。下拉列表上的验证似乎失败,错误为System.NullReferenceException:对象引用未设置为对象的实例。当我保存一条记录并故意将控件留空以测试验证时,就会发生这种情况。即使下拉列表本身有一个选择,也会发生这种情况

以下是我的部分观点:

<div class="editor">
    @Html.LabelFor(model => model.EmployeeID)
    @Html.DropDownListFor(model => model.EmployeeID, new SelectList(Model.Employees, "Value", "Text"))
    @Html.ValidationMessageFor(model => model.EmployeeID)
</div>
和我的viewmodel:

    public class VisitorLogViewModel
{
    public int Id { get; set; }

    [Display(Name = "Visitor Name")]
    public string VisitorName { get; set; }

    [Display(Name = "Company Name")]
    public string CompanyName { get; set; }

    [Required(ErrorMessage = "Employee ID is required.")]
    [Display(Name = "GB Employee")]
    public string EmployeeID { get; set; }

    [Display(Name = "Visit Reason")]
    public string VisitReason { get; set; }

    [Display(Name = "Time In")]
    public DateTime TimeIn { get; set; }

    [Display(Name = "Time Out")]
    public DateTime TimeOut { get; set; }

    [Display(Name = "GB Employee")]
    public string EmployeeName { get; set; }

    public IEnumerable Employees { get; set; }
    public VisitorLog VisitorLog { get; set; }
}
    public class VisitorLogViewModel
{
    public int Id { get; set; }

    [Required(ErrorMessage = "Visitor name is required.")]
    [MaxLength(128)]
    [Display(Name = "Visitor Name")]
    public string VisitorName { get; set; }

    [Required(ErrorMessage = "Company name is required.")]
    [MaxLength(128)]
    [Display(Name = "Company Name")]
    public string CompanyName { get; set; }

    [Required(ErrorMessage = "GB Employee is required.")]
    [MaxLength(16)]
    [Display(Name = "GB Employee")]
    public string EmployeeID { get; set; }

    [Required(ErrorMessage = "Visit Reason is required.")]
    [MaxLength(254)]
    [Display(Name = "Visit Reason")]
    public string VisitReason { get; set; }

    [Display(Name = "Time In")]
    public DateTime TimeIn { get; set; }

    [Display(Name = "Time Out")]
    public DateTime TimeOut { get; set; }

    [Display(Name = "GB Employee")]
    public string EmployeeName { get; set; }

    public SelectList Employees { get; set; }
}
以及我的部分验证模型:

    [MetadataType(typeof(VisitorLogMetaData))]
public partial class VisitorLog
{

}

public class VisitorLogMetaData
{
    [Required(ErrorMessage = "Visitor name is required.")]
    [MaxLength(128)]
    public string VisitorName { get; set; }

    [Required(ErrorMessage = "Company name is required.")]
    [MaxLength(128)]
    public string CompanyName { get; set; }

    [Required(ErrorMessage = "GB Employee is required.")]
    [MaxLength(128)]
    public string EmployeeID { get; set; }

    [Required(ErrorMessage = "Visit reason is required.")]
    [MaxLength(254)]
    public string VisitReason { get; set; }

    [Required(ErrorMessage = "Time in is required.")]
    public DateTime TimeIn { get; set; }

    [Required(ErrorMessage = "Time out reason is required.")]
    public DateTime TimeOut { get; set; }
}
最后是我的模型:

    public partial class VisitorLog
{
    public int Id { get; set; }
    public string VisitorName { get; set; }
    public DateTime TimeIn { get; set; }
    public DateTime TimeOut { get; set; }
    public string CompanyName { get; set; }
    public string EmployeeID { get; set; }
    public string VisitReason { get; set; }

    // Navigation properties
    [ForeignKey("EmployeeID")]
    public virtual Employee Employee { get; set; }
}
我读到MVC razor中有一个关于DropDownList for的bug,但我不知道这是否适用于我的情况。我尝试过一些解决方案,但它们对我不起作用。我使用的是4.5框架

谢谢

编辑:

我注意到一件事,当我提交页面时,错误在下拉元素上停止:

@Html.DropDownListFor(model => model.EmployeeID, new SelectList(Model.Employees, "Value", "Text"))

Model.Employees中的模型为空,就像提交页面时它正在失去绑定一样。

好的,我对我的类做了一些基本更改。首先,我更改了控制器中的post方法。以前我将模型传递给post,现在我将传递视图模型并将其映射到模型,然后通过我的存储库进行保存:

    //
    // POST: /VisitorLogs/Create

    [HttpPost]
    public ActionResult Create(VisitorLogViewModel visitorLogViewModel)
    {
        var e = iEmployeeRepository.GetAll();
        VisitorLog visitorLog = new VisitorLog();
        visitorLog.Id = visitorLogViewModel.Id;
        visitorLog.VisitorName = visitorLogViewModel.VisitorName;
        visitorLog.CompanyName = visitorLogViewModel.CompanyName;
        visitorLog.EmployeeID = visitorLogViewModel.EmployeeID;
        visitorLog.TimeIn = visitorLogViewModel.TimeIn;
        visitorLog.TimeOut = visitorLogViewModel.TimeOut;
        visitorLog.VisitReason = visitorLogViewModel.VisitReason;
        visitorLogViewModel.Employees = new SelectList(e, "EmployeeID", "EmployeeName");

        if (ModelState.IsValid)
        {
            iVisitorlogRepository.Add(visitorLog);
            iVisitorlogRepository.Save();
            return RedirectToAction("Search");
        } else {
            return View(visitorLogViewModel);
        }
    }
接下来,我必须将“必需”属性(验证)添加到viewmodel中:

    public class VisitorLogViewModel
{
    public int Id { get; set; }

    [Display(Name = "Visitor Name")]
    public string VisitorName { get; set; }

    [Display(Name = "Company Name")]
    public string CompanyName { get; set; }

    [Required(ErrorMessage = "Employee ID is required.")]
    [Display(Name = "GB Employee")]
    public string EmployeeID { get; set; }

    [Display(Name = "Visit Reason")]
    public string VisitReason { get; set; }

    [Display(Name = "Time In")]
    public DateTime TimeIn { get; set; }

    [Display(Name = "Time Out")]
    public DateTime TimeOut { get; set; }

    [Display(Name = "GB Employee")]
    public string EmployeeName { get; set; }

    public IEnumerable Employees { get; set; }
    public VisitorLog VisitorLog { get; set; }
}
    public class VisitorLogViewModel
{
    public int Id { get; set; }

    [Required(ErrorMessage = "Visitor name is required.")]
    [MaxLength(128)]
    [Display(Name = "Visitor Name")]
    public string VisitorName { get; set; }

    [Required(ErrorMessage = "Company name is required.")]
    [MaxLength(128)]
    [Display(Name = "Company Name")]
    public string CompanyName { get; set; }

    [Required(ErrorMessage = "GB Employee is required.")]
    [MaxLength(16)]
    [Display(Name = "GB Employee")]
    public string EmployeeID { get; set; }

    [Required(ErrorMessage = "Visit Reason is required.")]
    [MaxLength(254)]
    [Display(Name = "Visit Reason")]
    public string VisitReason { get; set; }

    [Display(Name = "Time In")]
    public DateTime TimeIn { get; set; }

    [Display(Name = "Time Out")]
    public DateTime TimeOut { get; set; }

    [Display(Name = "GB Employee")]
    public string EmployeeName { get; set; }

    public SelectList Employees { get; set; }
}
不确定这是否是最有效的方法,但现在一切都正常了。如果有人发现这种方法有问题,请告诉我