Вопрос по asp.net-mvc – Привязать значение маршрута к свойству объекта, который является частью viewmodel

1

Error: User Rate Limit Exceeded

            routes.MapRoute(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        );

Error: User Rate Limit Exceeded

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; }
    }

}

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

    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);    }
}
Error: User Rate Limit Exceeded Mathias F
Error: User Rate Limit Exceeded Jason
Error: User Rate Limit Exceeded Jason

Ваш Ответ

1   ответ
1

Error: User Rate Limit Exceeded

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);
    }
}
Error: User Rate Limit Exceeded Mathias F

Похожие вопросы