gpt4 book ai didi

asp.net-mvc-3 - MVC 3 使用修改后的查询字符串值重新加载当前页面

转载 作者:行者123 更新时间:2023-12-04 22:50:17 25 4
gpt4 key购买 nike

背景:

我有一个 MVC 布局(主) View ,它使用 @Html.RenderAction 在左侧导航面板中显示下拉列表。此下拉列表将显示在网站的所有页面上。
下拉列表包含在表单元素中,并在下拉列表更改时发布表单。

题:

现在,一旦表单发布,我需要使用查询字符串中附加的下拉列表的值重新加载当前页面的内容(用户当前所在的页面......)。这意味着替换先前选择的查询字符串中可能已经存在的值。

例子:

  • 用户导航到网站首页:

  • 网址:/Home/?dropdownvalue=blue

    此时,下拉列表显示“蓝​​色”为选中状态。用户将下拉列表中的值更改为“红色”。我需要使用以下网址重新加载页面 -

    /Home/?dropdownvalue=red
  • 用户移动到站点中的另一个页面:

  • 网址:/CustomerFavorite/?dropdown=red

    将下拉列表中的值从“红色”更改为“绿色”。

    'CustomerFavourite' 页面应在查询字符串中重新加载为 'Green'。

    我为冗长的帖子道歉。但是,考虑提供一些额外的信息来澄清这个问题。

    谢谢。

    最佳答案

    感谢 Darin 提供了查询字符串的 javascript 操作链接。但是,我想要一个服务器端解决方案,所以这是我实现它的方式 -

    public ActionResult _ColorSelection(ColorModel model)
    {
    string selectedColor = model.Color.Value;

    // Modify Querystring params...

    NameValueCollection querystring =
    HttpUtility.ParseQueryString(Request.UrlReferrer.Query); // Parse QS

    // If Querystring contains the 'color' param, then set it to selected value
    if (!string.IsNullOrEmpty(querystring["color"]))
    {
    querystring["color"] = selectedColor;
    }
    else // Add color key to querystring
    {
    querystring.Add("color", selectedColor);
    }

    // Create new url
    string url = Request.UrlReferrer.AbsolutePath
    + "?" + querystring.ToString();

    return Redirect(url); // redirect

    }

    关于asp.net-mvc-3 - MVC 3 使用修改后的查询字符串值重新加载当前页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7678843/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com