MVC 3搜索路由



我正在开发一个具有动态复选框数量和价格范围的搜索。我需要一条这样的路线:

/过滤器/属性/Attribute1、Attribute2、Attribute3/价格/1000-2000

这样做好吗?我该怎么走那条路?

routes.MapRoute(
    "FilterRoute",
    "filter/attributes/{attributes}/price/{pricerange}",
    new { controller = "Filter", action = "Index" }
);

在您的索引操作中:

public class FilterController: Controller
{
    public ActionResult Index(FilterViewModel model)
    {
        ...
    }
}

其中FilterViewModel:

public class FilterViewModel
{
    public string Attributes { get; set; }
    public string PriceRange { get; set; }
}

如果你想让你的FilterViewModel看起来像这样:

public class FilterViewModel
{
    public string[] Attributes { get; set; }
    public decimal? StartPrice { get; set; }
    public decimal? EndPrice { get; set; }
}

您可以为这个视图模型编写一个自定义模型绑定器,它将解析各种路由令牌。

如果你需要一个例子,请给我打电话。


更新:

根据要求,这里有一个示例模型绑定器,可用于将路由值解析为相应的视图模型属性:

public class FilterViewModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var model = new FilterViewModel();
        var attributes = bindingContext.ValueProvider.GetValue("attributes");
        var priceRange = bindingContext.ValueProvider.GetValue("pricerange");
        if (attributes != null && !string.IsNullOrEmpty(attributes.AttemptedValue))
        {
            model.Attributes = (attributes.AttemptedValue).Split(new [] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        }
        if (priceRange != null && !string.IsNullOrEmpty(priceRange.AttemptedValue))
        {
            var tokens = priceRange.AttemptedValue.Split('-');
            if (tokens.Length > 0)
            {
                model.StartPrice = GetPrice(tokens[0], bindingContext);
            }
            if (tokens.Length > 1)
            {
                model.EndPrice = GetPrice(tokens[1], bindingContext);
            }
        }
        return model;
    }
    private decimal? GetPrice(string value, ModelBindingContext bindingContext)
    {
        if (string.IsNullOrEmpty(value))
        {
            return null;
        }
        decimal price;
        if (decimal.TryParse(value, out price))
        {
            return price;
        }
        bindingContext.ModelState.AddModelError("pricerange", string.Format("{0} is an invalid price", value));
        return null;
    }
}

其将在CCD_ 3:中的CCD_

ModelBinders.Binders.Add(typeof(FilterViewModel), new FilterViewModelBinder());

最新更新