3

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

3 Answers 3

4

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)

Sign up to request clarification or add additional context in comments.

1 Comment

yes I tried this, but after my controller's action is called I use RedirectToAction method to redirect to different controller's action where in the view my results are listed
2

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.

Comments

1

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.