Fluent nhibernate 具有自定义类型成员的MVC 4模型不工作

Fluent nhibernate 具有自定义类型成员的MVC 4模型不工作,fluent-nhibernate,asp.net-mvc-4,Fluent Nhibernate,Asp.net Mvc 4,我正在开发一个MVC4应用程序,我遇到了一些麻烦。 以下是我的模型摘录: public class RegistryModel { [DisplayName("Registry id")] public int registryId { get; set; } [Required] [DataType(System.ComponentModel.DataAnnotations.DataType.Date)] [DisplayName("Receptio

我正在开发一个MVC4应用程序,我遇到了一些麻烦。 以下是我的模型摘录:

public class RegistryModel
{
    [DisplayName("Registry id")]
    public  int registryId { get; set; }

    [Required]
    [DataType(System.ComponentModel.DataAnnotations.DataType.Date)]
    [DisplayName("Reception date")]
    public  DateTime? receivedDate { get; set; }

    [Required]
    [DisplayName("Source")]
    public  Source source { get; set; }
}
源对象:

public class Source
{
    public virtual string sourceCode { get; set; }
    public virtual string fullName { get; set; }
    public virtual string shortName { get; set; }
    public virtual string type { get; set; }
    public virtual IList<Registry> registryList { get; set; }
}
公共类源代码
{
公共虚拟字符串源代码{get;set;}
公共虚拟字符串全名{get;set;}
公共虚拟字符串短名称{get;set;}
公共虚拟字符串类型{get;set;}
公共虚拟IList注册表列表{get;set;}
}
控制器:

public ActionResult Create()
{
    var sourceRepo = new Repository<DTOS.Source>(MvcApplication.UnitOfWork.Session);
    IEnumerable<SelectListItem> sourcesEnum = sourceRepo.FilterBy(x=>x.type.Equals("C")).Select(c => new SelectListItem { Value = c.sourceCode, Text = c.fullName });
    ViewBag.Sources = sourcesEnum;
    return View();
}
public ActionResult Create()
{
var sourceRepo=新存储库(mvcapapplication.UnitOfWork.Session);
IEnumerable sourcesEnum=sourceRepo.FilterBy(x=>x.type.Equals(“C”)).Select(C=>newselectListItem{Value=C.sourceCode,Text=C.fullName});
ViewBag.Sources=sourcesEnum;
返回视图();
}
最后一个观点是

@model Registry.Models.RegistryModel

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

<fieldset>
    <legend>New Registry</legend>      

    <div class="editor-label">
        @Html.LabelFor(model => model.source)
    </div>
    <div class="editor-field">
         @Html.DropDownListFor(Model => Model.source.sourceCode, (IEnumerable<SelectListItem>)ViewBag.Sources, String.Empty)
         @Html.ValidationMessageFor(model => model.source)
    </div>
@model Registry.Models.RegistryModel
@使用(Html.BeginForm()){
@Html.ValidationSummary(true)
新登记处
@LabelFor(model=>model.source)
@Html.DropDownListFor(Model=>Model.source.sourceCode,(IEnumerable)ViewBag.Sources,String.Empty)
@Html.ValidationMessageFor(model=>model.source)
如果我从dropdownlist中选择一个源,它可以正常工作。但是,如果我提交它而不选择任何源,我不会收到错误消息,即使它在模型中被注释为[Required]

在控制器级别调试HttpPost创建之后,我看到返回的RegistryModel的源成员已实例化,但其所有成员都为null(sourceCode、fullName等)

如果我在提交表单之前没有在dropdownlist中选择任何源,为什么MVC要实例化模型的源成员

我尝试通过以下方式修改vue:

@Html.DropDownListFor(Model => **Model.source**, (IEnumerable<SelectListItem>)ViewBag.Sources, String.Empty)
@Html.DropDownListFor(Model=>**Model.source**,(IEnumerable)ViewBag.Sources,String.Empty)
这一次,如果我未选择任何源而提交,则会出现错误消息,但如果我选择了一个源,则在提交后会出现另一条错误消息,表示“提交前选择的用户代码值无效”

任何帮助都将不胜感激!
B.

我相信模型绑定器将创建一个源对象,然后在提交表单时尝试填写它的值(无论是否存在)。RequiredAttribute的作用可能与您想象的不同。请参见此处: