0

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.

2 Answers 2

1

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

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

3 Comments

Tried adding productId but still value is null.
Use JSON.stringify({productID : $("#prodList").val()}) to pass it as the data as Bharath suggested, but make sure that the variable name on the action method matches the key value that you are sending (in your case productID). Please note that you do not need to put quotation marks around productID.
Did all of above.As mentioned gives runtime error in IE "JSON is undefined" but in chrome it works but the value is still null.Please refer to the change code above.
0

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

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

5 Comments

This does not work in IE 9 says JSON undefined and in chrome this works but the value in controller is still null
var selectedID = $("#prodList").val(); alert(selectedId) is alert null??? change your controller type to expect an int if .val() is an int type
@ C Sharper As mentioned I get the value of selected product in the alert. I am just not getting it in controller. It is not being passed from jquery function to Controller via $.ajax()
can you share the ajax request and response details from browser console?
@Bharath In Firebug console :http:mydomain/WorkFlowTest/ProductSubmission/. The Post shows JSON productID "Property" Source {"productId":"Property"} . The Response is the entire view in html format.

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.