3

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.

2
  • You want to GET and POST in one form? Commented Aug 13, 2012 at 20:19
  • No, pardon my excellent communication. I am trying to GET and use action parameters (which I suppose are RouteValues) in one form. Commented Aug 13, 2012 at 20:31

1 Answer 1

8

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" }))
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply. I suppose name is a routeValue, since I intend it to be an action parameter, but the first code segment ironically enough will pick up two other routeValues from the ViewData but omit name.

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.