【问题标题】:Change only some of the values of the current route仅更改当前路由的部分值
【发布时间】:2012-10-27 18:01:29
【问题描述】:

我的搜索页面有很多参数,在这个页面中我有一些当前搜索的过滤器。
我只想更改路线中的一些值。

一个例子:

http://www.example.com/{controller}/{action}/{brand}/{model}/{color}/{price}

在我的搜索页面中,我有一个可以更改颜色和价格的表单:

HTML:

@using (Html.BeginForm("action", "controller", FormMethod.Post))
{
    @Html.DropDownListFor(m => m.Color, Model.Colors)
    @Html.DropDownListFor(m => m.Price, Model.Prices)
    <input type="submit" value="Search" />

}

控制器:

[HttpPost]
public ActionResult Action(SearchModel search)
{
    //I can get the Price and Color
    string color = search.Color;
    string price = search.Price;

    //Now I want to get the values of brand and model

    return RedirectToRoute("Action",new
    {
         controller = "Controller",
         action = "Action",
         color = color,
         price = price,
         brand = ????,
         model = ????
    });

}

我的搜索有比这更多的参数...我不想将它们放在隐藏字段中并与模型一起发送:\

谢谢

【问题讨论】:

  • 您不想要什么?隐藏字段?如果这些是您的搜索参数,那么它们应该对用户可见,不是吗?或者是否存在用户输入可能会更改将发送用于搜索的参数的情况?
  • 嗨!我只想在控制器中捕获品牌和型号值(在本例中)。用户输入只会改变颜色和价格值。

标签: c# asp.net-mvc routes


【解决方案1】:

为什么不把它们放在 QueryString 中呢?然后它看起来像这样:

http://www.example.com/something.aspx?color=red&price=100

然后你可以拿起你想要的,忽略其余的,顺序无关紧要。

希望这能回答你的问题,说实话,我并没有真正理解。

【讨论】:

  • 对不起,如果我没有说清楚,但您的答案与我的路线不符:example.com{controller}/{action}/{brand}/{model}/{color}/{我想要的基本上是在用户提交表单后,用户将被重定向到具有新颜色和价格值的 url,并保持品牌和型号的值。
【解决方案2】:

我找到了解决办法。

我需要这样做:

@using (Html.BeginForm("action", 
                       "controller", 
                       new { brand = ViewContext.RouteData.Values["brand"],
                             model = ViewContext.RouteData.Values["model"] },
                             FormMethod.Post))
{
    @Html.DropDownListFor(m => m.Color, Model.Colors)
    @Html.DropDownListFor(m => m.Price, Model.Prices)
    <input type="submit" value="Search" />

}

然后在控制器中:

[HttpPost]
public ActionResult Action(SearchModel search)
{

    return RedirectToRoute("Action",new
    {
         controller = "Controller",
         action = "Action",
         color = search.Color,
         price = search.Price,
         brand = search.Brand,
         model = search.Model
    });

}

如果有人知道其他解决方案,请告诉我。 谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    • 2019-06-18
    • 2021-02-06
    • 2018-05-05
    • 1970-01-01
    • 2021-12-10
    • 2016-12-29
    相关资源
    最近更新 更多