2

In my razor view, I'm using a Html.BeginForm. In it I have two elements where when I set clic, should submit the form but I need to add an additional string parameter.

@using (Html.BeginForm("Index", "Quiz", FormMethod.Post, new { id = "form" }))
{
    ...
}

    [HttpPost]
    public ActionResult Index(string parameter, QuizCompletedViewModel q)  //FormCollection f)
    {            
        ...
        if (button.Equals("d"))
        {
            ...
            return RedirectToAction("ShowResults", new { testId = Quiz.QuizId, answeredTest = answeredId });
        }
        else
        {
            ...
            return RedirectToAction("Index", "Dashboard");
        }
    }

So, in my jquery function I utilize $("#element").submit() and the parameter parameter always is null (and that's normal). How can I add additional data for parameter using JQUERY?

NOTE: I'm not using AJAX.

5
  • What additional data? from where? what is #element - the form or some other element? Commented Feb 6, 2013 at 4:28
  • How are you passing the model? Commented Feb 6, 2013 at 4:29
  • @BhushanFirake Using the same whole model that my razor view Commented Feb 6, 2013 at 4:36
  • @tvanfosson #element is the form, sorry. I mean pass the data something like this: { parameter: "d", q: $("#form").serialize() } Commented Feb 6, 2013 at 4:36
  • possible duplicate of jQuery - add additional parameters on submit (NOT ajax) Commented Mar 25, 2014 at 13:02

3 Answers 3

6

On your form sumbit try like this:

        $("form").submit(function(){
           $.ajax({     
                type: 'POST',  
                url: "/Quiz/Index",
                dataType: "json",
                data: $("form").serialize() + '&parameter=param1' ,   
                success: function () { 
                    alert("Successful"); 
                }
                error: function(){
                    alert('error');
                }

            }); 
        });

Hope it helps.


Edit

Try like this in your jQuery function:

$('form').append('&param=a');
Sign up to request clarification or add additional context in comments.

4 Comments

Am I missing something? It's still passing a NULL value in parameter value: $("#form").append('button=a').submit();
can you see any querystring's in your url of browser when you submit the form
With you last answer, I can achieve more information since your solution: stackoverflow.com/questions/3761051/…
Wow! the number of times I monkeyed around with hidden elements, and that was all I had to do. Thanks!
6

You could add a hidden input with the name and value you require in your form. This value will be submitted along with all the other form items.

<input type="hidden" name="someName" value="someValue" />

Edit for comment:

If you give each of your submit buttons the value as an attribute you could grab it in the click handler and then append a hidden field with the value inside the form. Something like:

$(function(){
   $('#form input[type=submit]').click(function(e) {
       var val = this.getAttribute("data-param");
       $(this).closest('form').append('<input type="hidden" name="param" value="' + val + '" />");
   });
});

Untested, just a hunch. Hope that the form submit event does not get fired before the button click event. Also you will need to handle the enter event and key press etc.

Why don't you want to use ajax?

1 Comment

Well, imagine that I have two buttons in my razor view, one of them should send "d" as parameter and the another send "a" as parameter
4

Based on your comments, the easiest thing for you to do is make your buttons of type submit and give them a name. If they have a name, the value of the button that is clicked will be sent automatically with the form post, see: sending form's submit button's value?

Example:

When the a button is clicked, parameter will be posted with value a. Likewise for b and c. If the none button is clicked, no value will be sent and parameter will be null (shown for demonstration purposes, you may not need this).

@using (Html.BeginForm("Index", "Quiz", FormMethod.Post, new { id = "form" }))
{
    ...

    <input type="submit" value="a" name="parameter" />
    <input type="submit" value="b" name="parameter" />
    <input type="submit" value="c" name="parameter" />
    <input type="submit" value="none" />
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.