ASP.NET MVC - 获取Html.BeginForm()记住查询字符串PARAMS通过GET提交时, [英] ASP.NET MVC - Getting Html.BeginForm() to remember Querystring params when submitting via GET

查看:344
本文介绍了ASP.NET MVC - 获取Html.BeginForm()记住查询字符串PARAMS通过GET提交时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过Htm​​l.BeginForm()呈现的一种形式,它的存在作为母版页的组件,使其出现在应用程序的每一页上。我这样做使用Html.RenderAction()MVC中装配期货。这是一个简单的搜索形式,更新underneigh搜索表单本身相同成分的一些项目,并执行GET,以便搜索词出现在查询字符串。

I have a form rendered via Html.BeginForm(), it exists as a component in the Master page so that it appears on every page in the application. I have done this using Html.RenderAction() from Mvc Futures assembly. It's a simple search form that updates some items in the same component underneigh the search form itself, and performs a GET so that the search term appears in the querystring.

<div class="sideBarContent">
   <h2>Search Products</h2>

   <% using (Html.BeginForm(ViewContext.RouteData.Values["action"].ToString(),
         ViewContext.RouteData.Values["controller"].ToString(), FormMethod.Get)) { %>

      <fieldset>
         <legend>Search Products</legend>

         <div class="formRow">
            <label for="ProductsSearch">Search</label>
            <%= Html.TextBox("ProductsSearch") %>
         </div>

         <input type="submit" value="Search" class="button" />
       </fieldset>

   <% } %>

   <ul>
      // Products will eventually be listed here
   </ul>
</div>

我需要这种形式做到以下几点:

I need this form to do the following:

1)应追加产品搜索作为查询字符串参数(如执行GET它是任何当前页面。 example.com/?ProductsSearch=test example.com /书籍/小说?产品搜索=测试

1) It should perform a GET to whatever current page it is on appending 'ProductsSearch' as a querystring parameter (eg. example.com/?ProductsSearch=test or example.com/books/fiction?ProductsSearch=test)

2)应记住任何exising查询字符串参数已经在查询字符串,维护他们,你点击搜索按钮,例如后。 example.com/myOrders?page=2 搜索后,点击它应该去的 example.com/myOrders?page=2&ProductsSearch=test

2) It should remember any exising querystring parameters that are already in the querystring, maintaining them after you click Search button eg. example.com/myOrders?page=2 after Search click it should go to example.com/myOrders?page=2&ProductsSearch=test)

我可以得到它做1),但不能工作了2)。

I can get it to do 1) but can't work out 2).

我次感受通常为从GET和追​​加查询字符串PARAMS它需要有隐藏的表单字段,所以我可以写一个实用功能,可自动添加了一堆任何查询字符串值隐藏表单字段,但我想检查那有没有一种简单的方法,也许我会约了错误的方式。

I relise that normally for a from to GET and appending querystring params it needs to have hidden form fields, so I could write a utility function that automatically adds a bunch of hidden form fields for any querystring values, but I wanted to check that there's wasn't an easier approach, or maybe I'm going about it the wrong way.

干杯!

推荐答案

您需要做的隐藏的表单字段的方法。

You'll need to do the hidden form field method.

即使你可以在整个查询字符串附加在&lt的action属性的URL的末尾;形式&GT;标签,做GET表单提交时,浏览器不重视这一点。

Even if you could attach the entire querystring to the end of the URL in the action attribute of the <form> tag, browsers don't pay attention to this when doing GET form submissions.

您的方法是不是太困难;你想要做这样的事情:

Your method isn't too difficult; you'd want to do something like this:

public static string QueryStringAsHidden(this HtmlHelper helper)
{
    var sb = new StringBuilder();
    foreach (var key in HttpContext.Current.Request.QueryString.AllKeys)
    {
        if (! key.StartsWith("ProductSearch"))
            sb.Append(helper.Hidden(key, HttpContext.Current.Request.QueryString[key]));
    }
        return sb.ToString();
    }

我把.StartsWith()在那里,因为你不希望成为一个搜索页面,并提交搜索字符串两次(现在你可以prePEND寻呼和与产品搜索等搜索特定变量。

I put the .StartsWith() in there because you don't want to be on a search page and submit the search string twice (and now you can prepend paging and other search-specific variables with ProductSearch.

编辑:PS:为了得到张贴到当前页面的形式,你不必明确提供行动和控制器 - 您也可以发送空

PS: To get the form to post to the current page, you don't have to explicitly provide action and controller -- you can also send nulls.

EDIT2:为什么即使有一个辅助方法这么做呢? :)

Why even bother with a helper method? :)

<% HttpContext.Current.Request.QueryString.AllKeys.Where(k => !k.StartsWith("ProductSearch")).ToList().ForEach(k => Response.Write(Html.Hidden(k, HttpContext.Current.Request.QueryString[k]))); %>

詹姆斯

这篇关于ASP.NET MVC - 获取Html.BeginForm()记住查询字符串PARAMS通过GET提交时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆