0

I have the following:

<div>
    <form>
        <input id="toPay" type="text" readonly>
    </form>
</div>

<div>
    <form action="@Url.Action("Create", "Home", new {price = toPay.Value})">
        <input type="submit" value="Pay"/>
    </form>
</div>

I want to send the toPay value as a parameter to the Create action method that is of type HttpGet. How can I do something like this new {price = toPay.Value}

3
  • I didn't quite get what do you need. You may be confusing a few things. Forms are used for Post requests and links for Get request, which one do you need? Commented Aug 19, 2017 at 18:31
  • @PedroSouki The submit button should acts like a link. Commented Aug 19, 2017 at 19:22
  • To use a button to act as a link and use a HttpGet method, intercept the submit event with javascript and use jquery to make the Get request. But to Create you should use the Post request. That why I said that there is some confusion in your need. Commented Aug 19, 2017 at 19:35

4 Answers 4

1
<form method="get" action="@Url.Action("MyAction", "MyController")" >
   @Html.Textbox("toPay")
   <input type="submit" value="Pay"/>
</form>
Sign up to request clarification or add additional context in comments.

Comments

1

The input element needs to be inside the same form

<form action="@Url.Action("Create", "Home")">
<div>
    <input id="toPay" type="text" readonly>
</div>

<div>
    <input type="submit" value="Pay"/>

</div>
</form>

The data will then get passed across in the http body as a POST message.

Comments

0

Try this :

<div> 
<form>
   <input id="toPay" type="text" readonly/> //close input tag
</form>
</div>
<div>
<form>
 <input type="button" value="Pay" id = "pay"/> // make this change
</form>
</div>
<script>
    $('#pay').click(function() {
        $.ajax({
            url: @Url.Action("Create", "Home") + '?price =' + $('#toPay').val()
            type: 'POST',
            // ... other ajax options ...
        });
    });
</script>

Comments

0

and with the helpers it's more easier :

@using (Html.BeginForm("Create", "Home", FormMethod.Get))
{
    @Html.Textbox("toPay")
    <input type="submit" value="Pay" />
}

Comments

Your Answer

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