Bind a routevalue to a property of an object that is part of viewmodel

前端 未结 1 444
走了就别回头了
走了就别回头了 2021-01-07 08:22

I have the following route:

            routes.MapRoute(
            \"Default\",                                              // Route name
            \"{c         


        
相关标签:
1条回答
  • 2021-01-07 08:50

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题