views:

40

answers:

1

OK, I'm sorry if the tile of the question was unclear, and if you understand what I mean, please don't hesitate to help me think of a better one.

Anyway, I have a <input type="submit"> element for my form, and I want it to return the same URL as the URL of the page the element is on.

Currently, if I click the button, it takes me from /Calculate/Quadratic to /Calculate/QuadraticForm

In my controller for this view, I have the following code:

    [AcceptVerbs(HttpVerbs.Get)]
    public ViewResult Quadratic()
    {
        ViewData["Root1"] = "";
        ViewData["Root2"] = "";
        return View();
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ViewResult Quadratic(QuadCalc boss)
    {
        ViewData["Root1"] = x1;
        ViewData["Root2"] = x2;
        return View();
    }

And here is the markup and code for my Quadratic view page, which includes the form which includes the submit button I've been referring to:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>Quadratic</h2>

    <% using(Html.BeginForm("QuadraticForm", "Calculate")) %>
    <% { %>
    <div>
        a: <%= Html.TextBox("quadraticAValue") %>
        <br />
        b: <%= Html.TextBox("quadraticBValue") %>
        <br />
        c: <%= Html.TextBox("quadraticCValue") %>
        <br />
        <input type="submit" id="quadraticSubmitButton" value="Calculate!" />
        <br />
        <p><%= ViewData["Root1"] %></p>
        <p><%= ViewData["Root2"] %></p>
    </div>
    <% } %>
</asp:Content>

Therefore, all I really want is to have the submit button return the same page, but the HTTP post will aid the application in passing new ViewData. Unless I'm interpreting this all wrong.

+4  A: 

The problem is in your BeginForm method that calls the QuadraticForm action

<% using(Html.BeginForm("QuadraticForm", "Calculate")) %>

If you want to give an ID to the form you should use

<% using (Html.BeginForm("Quadratic", "Calculate", FormMethod.Post, new { id = "QuadraticForm" })) { %>

If you dont mind about the ID and want to just return to the same action just use

<% using(Html.BeginForm() %>

The parameters are:

  • The action name
  • The controller Name
  • The form method (get/post)
  • The form attributes
Lorenzo
Well, that's what I'm saying. However, you altered my quote. I said it takes you from `/Calculate/Quadratic` in the URL to `/Calculate/QuadraticForm` in the URL. It essentially appends `Form` to the end of the URL adding no slashes at all. What I'm saying is that I don't understand why it doesn't return the same view, and why it appends this form string to the end of the URL. I was wondering if the `<input type="submit">` element has any certain default routing or anything and is why the `Form` was appended.
BOSS
Ok. Could you please post some code from your view including the form?
Lorenzo
OK, sure sorry for the delay, let me update my original post.
BOSS
Please look at my answer update
Lorenzo
Hey, thanks a lot! I just picked the wrong overload then... xD
BOSS
You're welcome!
Lorenzo