0

I have html form in which I need to pass data to controller in MVC 4. I need to do it in jquery. The code is:

            <% using (Html.BeginForm("CopyEvaluationVersion", "Evaluations"))
            <input type="submit" value="Submit"  />

The controller

public ActionResult CopyEvaluationVersion(string copyEvaluationVersionId, string copyToYear)

The data is a hidden field

   <input type="hidden" id="copyEvaluationVersionId" name="copyEvaluationVersionId" value="<%= ViewData["evaluationVersionId"] %>" />

I need to pass hidden field "copyEvaluationVersionId" to copyEvaluationVersionId controller action ActionResult CopyEvaluationVersion(string copyEvaluationVersionId). I think I need to do it in jquery. But I am not sure how to do it.

7
  • Not really clear what your asking. If you want to submit the form and stay on the same page, then you need to use ajax. Commented Nov 20, 2015 at 21:54
  • I modify the post and read it again. I don't want to stay on the same page. Commented Nov 20, 2015 at 22:13
  • 1
    Then why do you think you need jquery? If your input for property copyEvaluationVersionId is inside the form tags then its value will be posted back when you submit the form. But based on the html your have shown, its value is null, so what is the point? Commented Nov 20, 2015 at 22:16
  • The point is that how to get copyEvaluationVersionId in controller action CopyEvaluationVersion. I think it should be in string copyEvaluationVersionId, but actually is not. The value of hidden field is preset by ViewData. Read the post again. Commented Nov 20, 2015 at 22:23
  • 1
    What? Read by last comment - if your post your form the value of the parameter copyEvaluationVersionId in your method will be the value of the hidden input (because the input has name="copyEvaluationVersionId"). Since its null in the view (its has value="") it will be null in the controller. Commented Nov 20, 2015 at 22:27

2 Answers 2

1

you can achieve this using an ajax call to the controller.

    function getDatatotheController() {
    var mode = document.getElementById('copyEvaluationVersionId').value;
    var parameters = { 'data': mode };

    $.ajax({
        type: "GET",
        url: '/YourControllerName/CopyEvaluationVersion',
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        data: parameters,
        success: successFunc,
        error: errorFunc
    });

    function successFunc(data) {
       alert(data);
    }

   function errorFunc() {
     alert(error);
   });
   }

and your controller method should look like this

public ActionResult CopyEvaluationVersion(string copyEvaluationVersionId)

you cant have two input parameters since you are only passing one parameter to the controller through the ajax call.

Note: if you are not passing any values to the view back from the controller "SuccesFunc" and "ErrorFunc" are not necessary.but if you need to to pass any values,back to the view it will be passed as "data" in the "SucceFunc()"

Hope this answer helps you..!

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

Comments

0

you can use this script to submit an HTML form using jquery

 $("#mybutton").click(function () {
//use your own selector
      $("form").submit();
});

Comments

Your Answer

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