Bondolin
Bondolin

Reputation: 3121

Use MVC Html.BeginForm with GET method and action parameters

I know I can use Html.BeginForm to POST with action parameters like this:

@using (Html.BeginForm("Details", "Home", new { name = Model.Name }))

and I can GET like this:

@using (Html.BeginForm("Details", "Home", FormMethod.Get))

But I can't seem to do both. How do I?

Update

Here is the entirety of the form in question:

@using (Html.BeginForm("Details", "Home", new { name = Model.Name }))
{
    <div style="text-align: center">
        @Html.DropDownList("viewMonth", 
                                  new List<SelectListItem>(ViewBag.MonthNames))
        @Html.TextBox("viewYear", Model.ViewYear, new {style = "width: 30px"})
        <input type="submit" value="Go"/>
        <br/>
        <br/>
    </div>
}

where viewMonth and viewYear are parameter names in Action/Details. If I try

@using (Html.BeginForm("Details", "Home", new { name = Model.Name }, 
                        FormMethod.Get))

it will pass viewMonth and viewYear, but not name.

Upvotes: 3

Views: 11508

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038800

But I can't seem to do both

You can do both ... you can use the overload you need

So for example if name is a routeValue:

@using (Html.BeginForm("Details", "Home", new { name = Model.Name }, 
                        FormMethod.Get))

and if name is an htmlAttribute of the form:

@using (Html.BeginForm("Details", "Home", FormMethod.Get, 
                        new { name = Model.Name }))

and if you want to use both routeValues and htmlAttributes:

@using (Html.BeginForm("Details", "Home", new { name = Model.Name }, 
                        FormMethod.Get, new { name = "foo_bar" }))

Upvotes: 8

Related Questions