1

I am trying to make an ajax sending data in JSON from a partial view. I get a System.ArgumentException: Invalid JSON primitive: undefined. When I evaluate the object in a browser data contains an int and two strings. Can anyone tell me what I am doing wrong?

Partial View

@model FTD.Models.FTDAccountExtended
@using (Html.BeginForm()) {
<fieldset>
<legend>Update Policy Number</legend>
@Html.HiddenFor(m => m.account.ftd_accountsid)
@Html.HiddenFor(m => m.OldPolicyNumber)
@Html.TextBoxFor(m => m.account.ftd_policyno)
<input type="button" value="update" id="update" />
</fieldset>
}

<script type="text/javascript">
 $(document).ready(function () {
     $("#update").click(function () {
         var myUrl = '@Url.Content("~/")' + '/Maintenance/UpdatePolicyNumber';
         var data = [{ 'ClientNumber': parseInt($("#account_ftd_accountsid").val()), 'OldPolicyNumber': $("#OldPolicyNumber").val(), 'NewPolicyNumber': $("#account_ftd_policyno").val()}];
         $.ajax({
             url: myUrl,
             type: 'POST',
             data: data,
             contentType: 'application/json; charset=utf-8',
             success: function (data) {
                 alert(data.message);
             },
             error: function (errMsg) {
                 alert("Error", errMsg);
             }
         });
     });
 });

The controller method is

public ActionResult UpdatePolicyNumber(int ClientNumber, string OldPolicyNumber, string NewPolicyNumber)
{
    var message = string.Format("UpdatePolicyNumber CN:{0} OP:{1} NP:{2}", ClientNumber, OldPolicyNumber, NewPolicyNumber);
    if (_log.IsDebugEnabled)
        _log.Debug(message);
    if (!string.IsNullOrEmpty(NewPolicyNumber) && ClientNumber > 0)
    {
        _entities = new CloseFTD_Entities();
        _entities.UpdatePolicyNumber(ClientNumber, OldPolicyNumber, NewPolicyNumber, User.Identity.Name);

     }
     return Json
        (
           new
           {
               message = message
           },
           JsonRequestBehavior.AllowGet
         );
}
7
  • can you show in here the structure of your json? Commented Mar 16, 2012 at 9:30
  • stackoverflow.com/questions/9699749/… i hope that link can help you.. Commented Mar 16, 2012 at 9:37
  • and this stackoverflow.com/questions/9730788/… Commented Mar 16, 2012 at 9:39
  • Essentially I want to pass the entry parameters of the controller method [{ 'ClientNumber': 83688, 'OldPolicyNumber': "WWW4567", 'NewPolicyNumber': "WWW1234" }] Commented Mar 16, 2012 at 9:58
  • wheer you want to parsing that data? in server side or client side? Commented Mar 16, 2012 at 10:01

3 Answers 3

1

I would just try posting the the data as a java script object (as Marc mentioned above) and remove the content type attribute.

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

1 Comment

Using Marcs change and removing the content type did it many thanks.
1
 success: function (data) {
                 alert(data.success);
             },

shouldn't this be

 success: function (data) {
                 alert(data.message);
             },

1 Comment

You quite right that is incorrect. However not actually triggering the controller, the error is in the data I am passing across.
1

Your problem is here

var data = [{ 'ClientNumber': parseInt($("#account_ftd_accountsid").val()), 'OldPolicyNumber': $("#OldPolicyNumber").val(), 'NewPolicyNumber': $("#account_ftd_policyno").val()}];

You are building an array - but your controller excepts the direct values, so just remove the [] brackets:

var data = { 'ClientNumber': parseInt($("#account_ftd_accountsid").val()), 'OldPolicyNumber': $("#OldPolicyNumber").val(), 'NewPolicyNumber': $("#account_ftd_policyno").val()};

That should work.

1 Comment

Tried removing the [] brackets, but still getting System.ArgumentException: Invalid JSON primitive: ClientNumber.

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.