views:

215

answers:

6

I have a form on a dialog box like so:

<form action="../ControlerFunction">
    <input type=text id="id1"/>
    <input type=text id="id2"/>
    <button type="submit"> OK </button>
    <button type="button" class="close"> Cancel </button>
</form>

When the form is submitted it hits my controller function, but how can I retrieve the values of the two text boxes?

Also when I change the form action to:

<form action="JavaScriptFunction();">
or:
<form action="JavaScriptFunction();return false;">

and I have my JavaScript on the same page as:

function JavaScriptFunction()
{
    alert("Hi!");
}

it does not hit the function. Am I missing something?

+1  A: 

In your controller add another action method that accepts an HTTP POST and takes in the form collection.

Like:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ControllerFunction(FormCollection collection)

Kindness,

Dan

Daniel Elliott
Hi Dan. When I do what you suggest it cannot find the function with "The resource cannot be found.". And when I take away the AcceptVerbs(HttpVerbs.Post) part is does hit the function but collection.count = 0!
Alex
I would add the POST method to your form tag and see if that has the same effect. Kindness, D
Daniel Elliott
A: 

Further to @Daniel.

You can either use MyAction[FormCollection collection] and then pull your values from the collection object.

Or, if you have a model that you passed to the view you can use TryUpdateModel() to propergate your model with the values from the form.

If you need more then post a comment and I'll add code.

Going home now so there will be a delay as I fight the traffic. :)

Oh, and welcome to SO.

griegs
A: 

Try considering this first:

  1. FORM Tag needs method(POST or GET);
  2. Your INPUT tags don't have name attributes which will be used for accessing the values.

Additional code:

<form name="form1" method="POST" onSubmit="JavaScriptFunction(); return false;">
        <input type=text id="id1" name="id1"/>
        <input type=text id="id2" name="id2"/>
        <button type="submit"> OK </button>
        <button type="button" class="close"> Cancel </button>
</form>

javascript:

function JavaScriptFunction()
{
    var id1Text = document.form1.elements["id1"].value; //get the value of id1
    var id2Text = document.form1.elements["id2"].value; //get the value of id2
    //do whatever you want here.
}
junmats
Great that worked. So what about my question where i'm trying to call a JavaScript function when it submits so that I can use Ajax to post back to the server?
Alex
oh sorry, i misunderstood your question. I'll try to post another answer later.
junmats
I've added additional code for your question.
junmats
+1  A: 

Correct html form tag syntax:

<form method="post" action="/controller/method/" onsubmit="yourJSFunction();">

...

This suits ALL server-side languages and technologies.

Sergei
Great thanks :)
Alex
A: 

I want to answer your first question.

how can i retreive the values of the two text boxes?

One answer is given by Daniel Elliot.
After giving you input tags a name attribute with the same value of the id attribut, you can access the values as parameters to your action method.

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ControllerFunction(String id1, String id2)

Your second question about java script is answerd by Sergei

Christian13467
A: 
<form name="form1" method="POST" onSubmit="JavaScriptFunction(); return false;">
        <input type=text id="id1" name="id1"/>
        <input type=text id="id2" name="id2"/>
        <button type="submit"> OK </button>
        <button type="button" class="close"> Cancel </button>
</form>

javascript:

function JavaScriptFunction()
{
    var id1Text = document.form1.elements["id1"].value; //get the value of id1
    var id2Text = document.form1.elements["id2"].value; //get the value of id2
    //do whatever you want here.
}
junmats
edit: onSubmit="JavaScriptFunction(); return false;"
junmats