1

I have used jquery ajax twice in a single view. The first works fine, but the second sends the zero value as the input parameter to the corresponding action,I don't know why?

<tr>
     <th>(@((int)ViewBag.BoonCount))</th>
         <td class="text-right">
             <input onchange="SetBoon()" class="form-control" type="number" name="Boon" id="MBoon" min="0" max="@((int)ViewBag.BoonCount)" value="" />
              <script>
               function SetBoon() {
                    var BoonVal = $('#MBoon').val();

                     $.ajax({
                            url: '/Users/SetBoon',
                            method: 'post',
                            contentType: 'json',
                            data: { fID: '@Factor.ID', BValue: BoonVal }
                          });

                    }
             </script>
          </td>

and it is my action code:

public string SetBoon(int fID, int BValue){}

The action receives zero as the input parameter.I even tested the @Factor.ID before entering the SetBoon function and had a valid value.

11
  • try it with removing the contenttype in $.ajax Commented Aug 16, 2017 at 8:59
  • you're sending factorID as a string and not an int. Remove the single quotes, i.e. data: { fID: @Factor.ID, BValue: BoonVal } Commented Aug 16, 2017 at 8:59
  • @ADyson I did your point, but still returns zero Commented Aug 16, 2017 at 9:13
  • Well, what is the value of Factor.ID when the page is first rendered? What value are you expecting? You should be able to see from your rendered page source what it's passing. Does BValue get populated correctly? Commented Aug 16, 2017 at 9:22
  • @ADyson This is the integer type and should send 31 Commented Aug 16, 2017 at 9:26

2 Answers 2

1

I must replace contentType by dataType in the ajax,if I remove contentType in the amount returned from the action to the view I encountered with another error.

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

Comments

0

json is not a contentType. It is a dataType for the response. If you want to specify the request data type, use

contentType: 'application/json'

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.