Gerry
Gerry

Reputation: 137

How to pass parameters to Html.BeginForm(action, controller)

View

@using (Html.BeginForm())
{
    <input name= "number1" /><br />
    <input type="submit"/>
}

Contoller

[HttpPost]
public ActionResult GetNumber(int number1)
{
}

works fine whereas the following code

View

@using (Html.BeginForm("SomeAction","SomeContoller"))
{
    <input name= "number1" /><br />
    <input type="submit"/>
}

Controller

[HttpPost]
public ActionResult SomeAction(int number1)
{
}

is not working. I tried without the parameter its working then, but i am not able to get the value for number1 to this contoller. How would I do that?.

Thanks for your answer :)

Upvotes: 2

Views: 17933

Answers (3)

Vasyl Korzhyk
Vasyl Korzhyk

Reputation: 11

Try this:

    @using (Html.BeginForm("SomeAction","SomeControler",new {number1 = 1}, FormMethod.Post))
    {                 
            <input type="submit" value="Submit" />
    }

Upvotes: 1

user3111579
user3111579

Reputation: 51

Do you use for controller name parameter something like "HomeController" or just "Home"? Because i think that only the prefix should be there.

@using (Html.BeginForm("MyAction", "Home"))
{
    <input name= "number1" /><br />
    <input type="submit"/>
}

Upvotes: 4

Stef Heyenrath
Stef Heyenrath

Reputation: 9820

Use <input id="number1" /> ?

Upvotes: 2

Related Questions