绑定routevalue一个对象的属性是视图模型的一部分 [英] Bind a routevalue to a property of an object that is part of viewmodel
问题描述
我有以下途径:
routes.MapRoute(
默认,//路线名称
{控制器} / {行动} / {ID},// URL带参数
新{控制器=家,行动=索引,ID =} //参数默认
);
和我使用的视图模型:
命名空间MvcApplication1.Models
{
公共类ProductViewModel
{ 公共ProductViewModel()
{
产品详情=新ProductInfo();
} 公共ProductInfo产品详情{搞定;组; } } 公共类ProductInfo
{
公共字符串名称{;组; }
公众诠释的ProductID {搞定;组; }
}}
我需要向Model.ProductDetail.ProductID路由的参数ID绑定的方法。
/产品/显示器/ 2导致:
Model.ProductDetail.ProductID == 2
我知道这看起来有点怪:这将是更简单,如果我的ViewModel只会
公共类ProductViewModel {公众诠释标识{获取;设置;}}
有关处理谐音我preFER使用聚集。我真的不能有ID在主视图模型类。
我是pretty舒尔,我需要实现我自己ModelBinder的,但我没有看到,我应该实现自己的规则。
我如何可以映射路径参数ID的财产Model.ProductDetail.ProductID?
编辑:
下面是如何做 -
公共类ProductModelBinder:DefaultModelBinder
{
保护覆盖无效BindProperty(
ControllerContext controllerContext,
ModelBindingContext BindingContext中,
System.ComponentModel.PropertyDescriptor的PropertyDescriptor
){
如果(bindingContext.ModelType == typeof运算(ProductViewModel)
&功放;&安培;的String.Compare(propertyDescriptor.Name,产品详情,真)== 0)
{
INT ID = int.Parse((字符串)controllerContext.RouteData.Values [ID]);
ProductViewModel的ProductView = bindingContext.Model为ProductViewModel;
productView.ProductDetail.ProductID = ID;
返回;
}
base.BindProperty(controllerContext,BindingContext中,PropertyDescriptor的); }
}
尝试是这样的(未测试):
公共类CustomModelBinder:DefaultModelBinder
{
保护覆盖无效BindProperty(ControllerContext controllerContext,ModelBindingContext的BindingContext,System.ComponentModel.PropertyDescriptor的PropertyDescriptor)
{
如果(bindingContext.ModelType == typeof运算(ProductInfo)
&功放;&安培;的String.Compare(propertyDescriptor.Name,产品ID,真)== 0)
{
VAR ID =(int)的controllerContext.RouteData.Values [ID]; VAR productInfo = bindingContext.Model为ProductInfo; productInfo.ProductID = ID; 返回;
} base.BindProperty(controllerContext,BindingContext中,PropertyDescriptor的);
}
}
I have the following route:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
and I use the ViewModel:
namespace MvcApplication1.Models
{
public class ProductViewModel
{
public ProductViewModel()
{
ProductDetail = new ProductInfo();
}
public ProductInfo ProductDetail { get; set; }
}
public class ProductInfo
{
public string Name { get; set; }
public int ProductID { get; set; }
}
}
I need a way to bind the parameter id of the route to Model.ProductDetail.ProductID.
/Products/Display/2 should lead to :
Model.ProductDetail.ProductID == 2
I know this looks a bit strange: It would be much simpler if my ViewModel would only be
public class ProductViewModel{ public int Id {get;set;}}
For handling partials I prefer to use aggregation. I realy cant have ID in the main ViewModel class.
I am pretty shure that I need to implement my own ModelBinder, but I dont see where I should implement my own rules.
How can I map the route parameter "id" to the property Model.ProductDetail.ProductID ?
EDIT:
Here is how its done -
public class ProductModelBinder: DefaultModelBinder
{
protected override void BindProperty(
ControllerContext controllerContext,
ModelBindingContext bindingContext,
System.ComponentModel.PropertyDescriptor propertyDescriptor
) {
if (bindingContext.ModelType == typeof(ProductViewModel)
&& String.Compare(propertyDescriptor.Name, "ProductDetail", true) == 0)
{
int id = int.Parse((string)controllerContext.RouteData.Values["id"]);
ProductViewModel productView = bindingContext.Model as ProductViewModel;
productView.ProductDetail.ProductID = id;
return;
}
base.BindProperty(controllerContext, bindingContext, propertyDescriptor); }
}
Try something like this (not tested):
public class CustomModelBinder : DefaultModelBinder
{
protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor)
{
if (bindingContext.ModelType == typeof(ProductInfo)
&& String.Compare(propertyDescriptor.Name, "ProductID", true) == 0)
{
var id = (int)controllerContext.RouteData.Values["id"];
var productInfo = bindingContext.Model as ProductInfo;
productInfo.ProductID = id;
return;
}
base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
}
}
这篇关于绑定routevalue一个对象的属性是视图模型的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!