asp.net-mvc – ASP.NET MVC模型绑定外键关系
发布时间:2020-05-24 14:45:33 所属栏目:asp.Net 来源:互联网
导读:是否可以将模型上的外键关系绑定到表单输入? 说我在Car和Manufacturer之间有一对多的关系.我想要一个更新Car的表单,其中包含用于设置Manufacturer的选择输入.我希望能够使用内置的模型绑定来做到这一点,但我开始认为我必须自己做. 我的动作方法签名如下所示
|
是否可以将模型上的外键关系绑定到表单输入? 说我在Car和Manufacturer之间有一对多的关系.我想要一个更新Car的表单,其中包含用于设置Manufacturer的选择输入.我希望能够使用内置的模型绑定来做到这一点,但我开始认为我必须自己做. 我的动作方法签名如下所示: public JsonResult Save(int id,[Bind(Include="Name,Description,Manufacturer")]Car car) 表单发布值Name,Description和Manufacturer,其中Manufacturer是int类型的主键.名称和描述设置正确,但不是制造商,这是有道理的,因为模型绑定器不知道PK字段是什么.这是否意味着我必须编写一个它知道这个的自定义IModelBinder?我不确定这是如何工作的,因为我的数据访问存储库是通过每个Controller构造函数上的IoC容器加载的. 解决方法这是我的看法 – 这是一个自定义模型绑定器,当被问到GetPropertyValue时,查看属性是否是我的模型程序集中的对象,并且具有IRepository<>在我的NInject IKernel中注册.如果它可以从Ninject获取IRepository,它会使用它来检索外键对象.public class ForeignKeyModelBinder : System.Web.Mvc.DefaultModelBinder
{
private IKernel serviceLocator;
public ForeignKeyModelBinder( IKernel serviceLocator )
{
Check.Require( serviceLocator,"IKernel is required" );
this.serviceLocator = serviceLocator;
}
/// <summary>
/// if the property type being asked for has a IRepository registered in the service locator,/// use that to retrieve the instance. if not,use the default behavior.
/// </summary>
protected override object GetPropertyValue( ControllerContext controllerContext,ModelBindingContext bindingContext,PropertyDescriptor propertyDescriptor,IModelBinder propertyBinder )
{
var submittedValue = bindingContext.ValueProvider.GetValue( bindingContext.ModelName );
if ( submittedValue == null )
{
string fullPropertyKey = CreateSubPropertyName( bindingContext.ModelName,"Id" );
submittedValue = bindingContext.ValueProvider.GetValue( fullPropertyKey );
}
if ( submittedValue != null )
{
var value = TryGetFromRepository( submittedValue.AttemptedValue,propertyDescriptor.PropertyType );
if ( value != null )
return value;
}
return base.GetPropertyValue( controllerContext,bindingContext,propertyDescriptor,propertyBinder );
}
protected override object CreateModel( ControllerContext controllerContext,Type modelType )
{
string fullPropertyKey = CreateSubPropertyName( bindingContext.ModelName,"Id" );
var submittedValue = bindingContext.ValueProvider.GetValue( fullPropertyKey );
if ( submittedValue != null )
{
var value = TryGetFromRepository( submittedValue.AttemptedValue,modelType );
if ( value != null )
return value;
}
return base.CreateModel( controllerContext,modelType );
}
private object TryGetFromRepository( string key,Type propertyType )
{
if ( CheckRepository( propertyType ) && !string.IsNullOrEmpty( key ) )
{
Type genericRepositoryType = typeof( IRepository<> );
Type specificRepositoryType = genericRepositoryType.MakeGenericType( propertyType );
var repository = serviceLocator.TryGet( specificRepositoryType );
int id = 0;
#if DEBUG
Check.Require( repository,"{0} is not available for use in binding".FormatWith( specificRepositoryType.FullName ) );
#endif
if ( repository != null && Int32.TryParse( key,out id ) )
{
return repository.InvokeMethod( "GetById",id );
}
}
return null;
}
/// <summary>
/// perform simple check to see if we should even bother looking for a repository
/// </summary>
private bool CheckRepository( Type propertyType )
{
return propertyType.HasInterface<IModelObject>();
}
}
您显然可以将Ninject替换为您的DI容器和您自己的存储库类型. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – MVC应用程序调试错误:viewstate MAC的验证
- asp.net – 如何正确地大写希腊字在.NET?
- asp.net-mvc – 如何将我的对象保存回LINQ to SQL中的数据库
- asp.net – Inno安装IIS安装和配置
- 强制ASP.Net MVC Bundle以某种顺序呈现javascript文件
- asp.net-mvc-4 – MVC 4 Razor如果拆分div标签
- 在asp.net mvc 3中实现FilterAttribute,IActionFilter和继承
- asp.net-mvc – 如何阻止ASP.Net MVC Html.ActionLink使用现
- asp.net-mvc – 此模板试图加载组件程序集Microsoft.Visual
- asp.net-mvc – 是否有一种方法来创建一个ActionLink与HTML
推荐文章
站长推荐
- asp.net – 模型在表单发布到控制器时始终为NULL
- asp.net – 为什么IFormFile显示为null,我该如何
- .net – 实体框架遇到的最大池大小4.3
- asp.net-mvc – 如何在使用类型化视图时在Action
- asp.net-mvc – 我应该为ASP.Net MVC项目使用什么
- asp.net-mvc – EntityFramework.SqlServer未在W
- asp.net-mvc-3 – MVC3 / Razor缩略图/调整大小图
- asp.net-mvc-3 – 为什么_ViewStart.cshtml访问V
- Asp.net Core 1.1 升级后操作mysql出错的解决办法
- asp.net-mvc – 如何在Html.RenderAction(MVC3)中
热点阅读
