Asp.net mvc 3 ASP.NET MVC3中奇怪的数据绑定行为?

Asp.net mvc 3 ASP.NET MVC3中奇怪的数据绑定行为?,asp.net-mvc-3,data-binding,Asp.net Mvc 3,Data Binding,我有以下域对象: public class Foo { public Id {get; set;} **public BarId {get; set;}** public virtual Bar {get; set;} public string Name {get; set;} } public class Bar { public Id {get; set;} public string Name

我有以下域对象:

public class Foo { public Id {get; set;} **public BarId {get; set;}** public virtual Bar {get; set;} public string Name {get; set;} } public class Bar { public Id {get; set;} public string Name {get; set;} } 公开课Foo { 公共Id{get;set;} **公共BarId{get;set;}** 公共虚拟条{get;set;} 公共字符串名称{get;set;} } 公共类酒吧 { 公共Id{get;set;} 公共字符串名称{get;set;} } 视图使用的视图模型如下所示:

public class FooEditorModel { public Foo {set; get;} public SelectList Bars { get; set; } } @model FooEditorModel @Html.ValidationSummary( true ) @Html.HiddenFor( model => model.Foo.Id ) @Html.LabelFor( model => model.Foo.BarId, "Bar" ) @Html.DropDownListFor( model => model.Foo.BarId, Model.Bars ) @Html.ValidationMessageFor( model => model.Foo.BarId ) [HttpPost] public virtual ActionResult Save( FooEditorModel model ) { if ( ModelState.IsValid ) { Foo foo = MakeAModel( model ); FooRepository.Save( foo ); } } Foo MakeAModel( model ) { Foo foo = MakeAFoo(); **foo.BarId = model.Foo.BarId;** return foo; } 公共类FooEditorModel { 公共Foo{set;get;} 公共选择列表栏{get;set;} } 视图如下所示:

public class FooEditorModel { public Foo {set; get;} public SelectList Bars { get; set; } } @model FooEditorModel @Html.ValidationSummary( true ) @Html.HiddenFor( model => model.Foo.Id ) @Html.LabelFor( model => model.Foo.BarId, "Bar" ) @Html.DropDownListFor( model => model.Foo.BarId, Model.Bars ) @Html.ValidationMessageFor( model => model.Foo.BarId ) [HttpPost] public virtual ActionResult Save( FooEditorModel model ) { if ( ModelState.IsValid ) { Foo foo = MakeAModel( model ); FooRepository.Save( foo ); } } Foo MakeAModel( model ) { Foo foo = MakeAFoo(); **foo.BarId = model.Foo.BarId;** return foo; } @模型FooEditorModel @Html.ValidationSummary(true) @Html.HiddenFor(model=>model.Foo.Id) @LabelFor(model=>model.Foo.BarId,“Bar”) @DropDownListFor(model=>model.Foo.BarId,model.bar) @Html.ValidationMessageFor(model=>model.Foo.BarId) 控制器如下所示:

public class FooEditorModel { public Foo {set; get;} public SelectList Bars { get; set; } } @model FooEditorModel @Html.ValidationSummary( true ) @Html.HiddenFor( model => model.Foo.Id ) @Html.LabelFor( model => model.Foo.BarId, "Bar" ) @Html.DropDownListFor( model => model.Foo.BarId, Model.Bars ) @Html.ValidationMessageFor( model => model.Foo.BarId ) [HttpPost] public virtual ActionResult Save( FooEditorModel model ) { if ( ModelState.IsValid ) { Foo foo = MakeAModel( model ); FooRepository.Save( foo ); } } Foo MakeAModel( model ) { Foo foo = MakeAFoo(); **foo.BarId = model.Foo.BarId;** return foo; } [HttpPost] 公共虚拟操作结果保存(FooEditorModel模型) { if(ModelState.IsValid) { Foo-Foo=MakeAModel(模型); foosepository.Save(foo); } } Foo MakeAModel(模型) { Foo-Foo=MakeAFoo(); **foo.BarId=model.foo.BarId** 返回foo; } 相当标准的代码,有点简化。
问题是,如果我在Foo中使用Bar,而不是BarId,则ModelState无效(因为Bar.Name)。
我发现它的设计非常糟糕,被迫在根对象中同时具有子Id和对子对象的引用

这是MS MVC 3中数据绑定工作的唯一方法吗