resthere
resthere

Reputation: 129

Not getting parameter value in controller by ajax call -

The application is in .Net framework 3.5 and MVC 2 for .NetFramework 3.5

The view has the jquery function click on button Add Product :

This calls the controller WorkFlowTest with Action ProductSubmission

The part of  controller is:

  [AcceptVerbs(HttpVerbs.Post)]

     public ActionResult ProductSubmission(string prodSelected,  
          ViewModels.WorkFlowTestViewModel SubmissionModelView)
     {
        SubmissionModelView.selectedProd = prodSelected   ;
        return View("Submission", SubmissionModelView);
     }

The part of  view is:

    <script src="/Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script src="/Scripts/jquery-1.4.1.min-vsdoc.js" type="text/javascript"></script>
    <script src="/Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
    <script type="text/javascript">
      $(function() {
       $("#addProd").click(function() {
        var selectedID = $("#prodList").val();
        $.ajax({
        url: "/WorkFlowTest/ProductSubmission/" ,
            type: 'POST',
            data: {"productID" : $("#prodList").val()},
            contentType: 'application/json; charset=utf-8',
            success: function(data) {
                //alert(data.success);
                alert("success");
            },
            error: function() {
                alert("error");
            }
        });
    });
});

 <select id ="prodList" style = "width:150px">
                        <option value ="GL " >GL </option>
                        <option value ="Property"   
                                     selected="selected">Property </option>
                        <option value ="Package" >Package </option>
                        <option value ="Island" >Island </option>
                        </select>
                     </td>

<td style="width: 313px"><input type ="button" id="addProd" value ="Add Product" />

In the jQuery function I get the selected value of the drop down "prodList" but the parameter, prodSelected in action, ProductSubmission of the controller does not get the value; Though I have specified it in "data:" section of the Ajax call.

Can some one tell if I am missing or how do I debug to see why the parameter is not getting passed. Or is there any other way to get the selected value in controller.

I added the meta tag

<head runat="server">
 <meta http-equiv="X-UA-Compatible" content="IE=edge;chrome=1" />
<title> Submission </title>

Changed The function data: JSON.stringify({"productID" : $("#prodList").val()}),

This does not work in IE 9 says JSON undefined and in chrome this works but the value is null.

The changes are as: The controller is: public ActionResult ProductSubmission(string productID, ViewModels.WorkFlowTestViewModel SubmissionModelView)

I added the meta tag

<head runat="server">
 <meta http-equiv="X-UA-Compatible" content="IE=edge;chrome=1" />
<title> Submission </title>

The jquery function:

data: JSON.stringify({productID : $("#prodList").val()}),

As mentioned gives runtime error in IE "JSON is undefined" but in chrome it works but the value is still null.

Upvotes: 0

Views: 2363

Answers (2)

Raenix
Raenix

Reputation: 66

Change your action method so that it accepts the parameter string productID instead of string prodSelected.

Upvotes: 1

Bharath
Bharath

Reputation: 519

JSON.stringify() the data when you are having contentType as application/json

data: JSON.stringify({"productID" : $("#prodList").val()})

Upvotes: 0

Related Questions