0

I have a .cshtml page with several buttons like this:

<input id="btnViewHistory" type="submit" name="postFunction" value="View History" />
<input id="btnComments" type="submit" name="postFunction" value="Comments" />

When the form is submitted, the parameter "postFunction" is passed to the controller and I can check what button was pressed:

[HttpPost]
    public ActionResult LabApprovals(ModelApproval model, int page, string postFunction)
    {
        if (postFunction == null)
        {
          ...
        }
        else if (postFunction == "View History")
        {
            ...
        }
        else if (postFunction == "Comments")
        {
            ...
        }
        else
        {
            return View(model);
        }
    }

So if you click the "View History" button, when the controller is hit postFunction="View History".

I need to submit the form from Javascript for another reason other than a button press. I have the Javascript to submit the form, but how do I pass a parameter?I'm trying to get postFunction to have a value such as "Changed Page" when the controller is reached.

    $("#txtPage").kendoNumericTextBox({
    change: function () {
        $("#formLabApprovals").submit();
    }
    });

3 Answers 3

4

Often one will put script-supplied values in a hidden field so they flow through to your controller but are invisible to the user. Set the field's value in your existing script.

<input type="hidden" name="fieldFromScript" />

Sign up to request clarification or add additional context in comments.

Comments

1

You can use the .val(*value*) method to assign the value attribute programmatically.

$("#txtPage").kendoNumericTextBox({
change: function () {
    $("#formLabApprovals").val("Changed Page");
    $("#formLabApprovals").submit();
} });

jQuery(#id).val() VS getElementById(#id).value is another article with some related information about using .val().

Comments

1

This is similar to both the other answers, but I thought I'd post this as well. It's what I finally did to get it to work. I found this page that was helpful: jQuery - add additional parameters on submit (NOT ajax).

var input = $("<input>")
    .attr("type", "hidden")
    .attr("name", "postFunction").val("ChangePage");
$('#formLabApprovals').append($(input));
$("#formLabApprovals").submit();

1 Comment

I know it's an older post, but it still keeps on giving. Thanks for the rewrite.

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.