Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC3 JSON Ajax error
    text
    copied!<p>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?</p> <p>Partial View</p> <pre><code>@model FTD.Models.FTDAccountExtended @using (Html.BeginForm()) { &lt;fieldset&gt; &lt;legend&gt;Update Policy Number&lt;/legend&gt; @Html.HiddenFor(m =&gt; m.account.ftd_accountsid) @Html.HiddenFor(m =&gt; m.OldPolicyNumber) @Html.TextBoxFor(m =&gt; m.account.ftd_policyno) &lt;input type="button" value="update" id="update" /&gt; &lt;/fieldset&gt; } &lt;script type="text/javascript"&gt; $(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); } }); }); }); </code></pre> <p></p> <p>The controller method is </p> <pre><code>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) &amp;&amp; ClientNumber &gt; 0) { _entities = new CloseFTD_Entities(); _entities.UpdatePolicyNumber(ClientNumber, OldPolicyNumber, NewPolicyNumber, User.Identity.Name); } return Json ( new { message = message }, JsonRequestBehavior.AllowGet ); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload