asp.net mvc提交表单并在url中显示所有表单参数

发布于 2024-12-27 03:35:51 字数 302 浏览 1 评论 0原文

我的视图上有一个表单和一个相应的提交按钮。该表单负责选择某些优惠搜索的选项。主页网址如下所示:

http://localhost/

当我单击提交按钮时,将调用适当的控制器操作。但是,我希望所有表单的参数都在网址中公开(因此可以在两个人之间共享链接,例如,他们将获得相同的结果)。 例如,我怎样才能实现(例如)类似的事情:

http://localhost/?startDate=20120215&endDate=20120230&catalog=ISA

I have a form on my view and a corresponding submit button. The form is responsible for selecting options for some offers search. The homepage url looks as follows:

http://localhost/

when I click submit button, an appropriate controller's action is called. However, I would like all form's parameters to be exposed in the url (so there will be a possibility to share the link between 2 persons for instance and they will have the same results).
So for instance, how can I achieve (for instance) something like that:

http://localhost/?startDate=20120215&endDate=20120230&catalog=ISA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晨敛清荷 2025-01-03 03:35:51

如果您使用表单的方法 GET,则所有变量都将成为查询字符串的一部分。

您可以使用此重载来更改表单的请求类型:

FormExtensions.BeginForm Method (HtmlHelper, String, String, FormMethod)

或者,如果您使用 RedirectToAction,则可以将参数作为对象传递:

Controller.RedirectToAction 方法(字符串、对象)

If you make the form's method GET, all of the variables will be part of the query string.

You can use this overload to change the Form's request type:

FormExtensions.BeginForm Method (HtmlHelper, String, String, FormMethod)

Or, if you're using RedirectToAction, you can pass the parameters as an object:

Controller.RedirectToAction Method (String, Object)

坏尐絯 2025-01-03 03:35:51

您应该指定使用 GET http 请求(而不是 post)提交表单,并指定您希望重定向到的操作,以便不必使用 RedirectToAction。

例如:

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult NextAction(IndexModel model)
    {
        return View();
    }
}

模型:

public class IndexModel
{
    public string StartDate { get; set; }

    public string EndDate { get; set; }

    public string Catalog { get; set; }
}

视图:

@model MvcApplication22.Models.IndexModel

@using (Html.BeginForm("NextAction", "Home", FormMethod.Get))
{
    <p>Start Date: @Html.EditorFor(m => m.StartDate)</p>
    <p>End Date: @Html.EditorFor(m => m.EndDate)</p>
    <p>Catalog: @Html.EditorFor(m => m.Catalog)</p>
    <input type="submit" value="submit" />
}

但请注意,在 GET http 请求中对系统进行任何更改并不是最佳实践。如果要进行任何更改,则应在 POST 请求中执行这些更改。

You should specify that the form submits using a GET http request (rather than post) and specify the action that you wish to redirect to so that you do not have to use RedirectToAction.

For example:

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult NextAction(IndexModel model)
    {
        return View();
    }
}

Model:

public class IndexModel
{
    public string StartDate { get; set; }

    public string EndDate { get; set; }

    public string Catalog { get; set; }
}

View:

@model MvcApplication22.Models.IndexModel

@using (Html.BeginForm("NextAction", "Home", FormMethod.Get))
{
    <p>Start Date: @Html.EditorFor(m => m.StartDate)</p>
    <p>End Date: @Html.EditorFor(m => m.EndDate)</p>
    <p>Catalog: @Html.EditorFor(m => m.Catalog)</p>
    <input type="submit" value="submit" />
}

But note that it is not best practise to make any changes to your system in the GET http request. If any changes are to be made then these should be performed in the POST request.

两人的回忆 2025-01-03 03:35:51

使用 httpGET 方法提交表单,因此表单的渲染输出将类似于

<form method="get">...</form>

submit the form using http's GET method, so your form's rendered output would be like

<form method="get">...</form>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文