mmmoustache
mmmoustache

Reputation: 2323

Post form data to url in Razor

I'm trying to append data from a form to the Url of my page using Razor (adding a query string). I've done this in ASP.NET Web Controls okay, but I would prefer to do this in Razor if it is possible?

Here's a very basic version of my Razor script but currently the '@test' variable is empty on post:

@{
    <form id="test" method="post">
        <input type="text" />
        <input type="submit" value="Submit" />
    </form>

    if(IsPost){
        var test = Request.Form["test"];
        Response.Redirect(@Model.Url + "?test=" + @test);
    }
}

As a sidenote, is there a way of achieving this without a POST method at all?

Upvotes: 0

Views: 3268

Answers (1)

Eugene Trofimenko
Eugene Trofimenko

Reputation: 1631

As I understand you want to add input value in your test variable. You should define id for input[text] or you should change it to

Page:

@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.GET, null))
{
   <input type='text' name'test' id='test' />
   <input type="submit" value="Submit" />
}

or your code

@{
    <form method="get" action="@Model.Url">
        <input type="text" name="test" id="test" />
        <input type="submit" value="Submit" />
    </form>    
}

P.S. I'm not clearly understand your code, because you set POST method and want to process GET method.

Upvotes: 3

Related Questions