MVC4-JQuery/Ajax post返回[ArgumentException:无效的JSON原语:类别。]
我正在尝试使用ajax将表单发布到MVC4中的特定操作。它进入控制器/动作,但抛出NullReferenceException,即使该动作确实接收到数据 行动如下:MVC4-JQuery/Ajax post返回[ArgumentException:无效的JSON原语:类别。],jquery,ajax,asp.net-mvc-4,Jquery,Ajax,Asp.net Mvc 4,我正在尝试使用ajax将表单发布到MVC4中的特定操作。它进入控制器/动作,但抛出NullReferenceException,即使该动作确实接收到数据 行动如下: [HttpPost] public ActionResult EditPlayer(PlayerDisplay myPlayer) { updatePlayer(myPlayer); //updatePlayer() does all the work of updating
[HttpPost]
public ActionResult EditPlayer(PlayerDisplay myPlayer)
{
updatePlayer(myPlayer); //updatePlayer() does all the work of updating the player information.
bool json = true;
return Json(json);
}
这是表格
@模型WebPlayerMonitor.Models.PlayerDisplay
<fieldset>
<legend id="edit-player-account"><b>Account: </b>@Model.account</legend>
<form id="editForm">
...
<button id="btn-edit" class="button round medium">Save & Close</button>
<a class="close-reveal-modal">×</a>
</form>
</fieldset>
模型:
public class PlayerDisplay
{
[StringLength(maximumLength: 30)]
[Required]
[Display(Name = "Account")]
public string account { get; set; }
[DataType(DataType.Text)]
[StringLength(maximumLength: 255)]
[Display(Name = "Link Iovation")]
public string link_Iovation { get; set; }
[Display(Name = "Deposit Type")]
public string Deposittype { get; set; }
[Display(Name = "8-Ball %")]
public float? eight_Ball { get; set; }
[DataType(DataType.Text)]
[StringLength(maximumLength: 50)]
[Display(Name = "Location")]
public string location { get; set; }
[Display(Name = "Lifetime")]
public float? lifetime { get; set; }
[Display(Name = "Master")]
public bool master { get; set; }
[DataType(DataType.Text)]
[StringLength(maximumLength: 50)]
public string Agent { get; set; }
[Display(Name = "B-Line")]
public bool b_line { get; set; }
[DataType(DataType.MultilineText)]
[StringLength(maximumLength: 1020)]
[Display(Name = "Notes")]
public string notes { get; set; }
[Required]
public string Status { get; set; }
[Required]
public string Category { get; set; }
[Display(Name = "Submitted")]
public DateTime? date { get; set; }
public string Office { get; set; }
[Display(Name = "Submitted By")]
public string User { get; set; }
[Display(Name = "Last Updated By")]
public string User_LastUp { get; set; }
[DataType(DataType.MultilineText)]
[StringLength(maximumLength: 200)]
[Required]
[Display(Name = "Submit Notes")]
public string shortNotes { get; set; }
[RegularExpression(@"\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b",
ErrorMessage = "Enter a valid IP format.")]
[Display(Name = "IP Address")]
public string ip_Address { get; set; }
public bool? state { get; set; }
[Display(Name = "Monitor Date")]
public DateTime? monitorDate { get; set; }
[Display(Name = "RFCheck Date")]
public DateTime? RFCheckDate { get; set; }
public string categoryMultiple { get; set; }
public PlayerDisplay()
{
}
}
来自Chrome开发者工具的标题信息:
Remote Address:xx.0.222.114:48
Request URL:http://crislife.im.priv:48/playermonitor/PlayerMonitor/EditPlayer
Request Method:POST
Status Code:500 Internal Server Error
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate
Accept-Language:en-US,en;q=0.8,es;q=0.6
Connection:keep-alive
Content-Length:599
Content-Type:application/json
Cookie:ASP.NET_SessionId=h40g5ar5yhrfgfsv4re1l0a1
Host:crislife.im.priv:48
Origin:http://crislife.im.priv:48
Referer:http://crislife.im.priv:48/playermonitor/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36
X-Requested-With:XMLHttpRequest
Request Payload
Category=8Ball+Edge&Status=Ready+for+Check&Deposittype=Money+Bookers&Office=BETCRIS+INT+BKR&Agent=my+agentsssd&master=true&link_Iovation=soelinkssssss&eight_Ball=1&location=earthssssss&lifetime=2&ip_Address=123.123.123.125&b_line=true¬es=awesome+new+notesssssss5s&account=NEWAWESOME&state=True&monitorDate=9%2F24%2F2014+3%3A50%3A12+PM&shortNotes=quick%2Fnew+notes%2Fsome+more+notes%2Fandmorenotes%2Fjl%3Bjkl%3Bjasdf%2Fadding+to+it&RFCheckDate=9%2F24%2F2014+3%3A50%3A12+PM&date=9%2F22%2F2014+9%3A33%3A01+AM&User=ADEN&User_LastUp=ADEN&categoryMultiple=8Ball+Edge%2CFraud+Check%2CNew+Deposit%2CSharp
Response Headersview source
Cache-Control:private
Content-Length:11536
Content-Type:text/html; charset=utf-8
Date:Mon, 20 Oct 2014 21:45:43 GMT
Server:Microsoft-IIS/7.5
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
我得到的错误是:
[ArgumentException: Invalid JSON primitive: Category.]
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject() +745
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth) +362
System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer) +115
System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit) +49
System.Web.Mvc.JsonValueProviderFactory.GetValueProvider(ControllerContext controllerContext) +242
System.Web.Mvc.<>c__DisplayClassc.<GetValueProvider>b__7(ValueProviderFactory factory) +28
System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +248
System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +165
System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +536
System.Linq.Enumerable.ToList(IEnumerable`1 source) +80
System.Web.Mvc.ValueProviderFactoryCollection.GetValueProvider(ControllerContext controllerContext) +347
System.Web.Mvc.ControllerBase.get_ValueProvider() +66
System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +81
System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +153
System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__1e(AsyncCallback asyncCallback, Object asyncState) +955
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +146
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +166
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag) +27
System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__17(AsyncCallback asyncCallback, Object asyncState) +50
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +146
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +166
System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +497
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +146
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +166
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag) +27
System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +409
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__2(AsyncCallback asyncCallback, Object asyncState) +144
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +146
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +166
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag) +27
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +364
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +12650919
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288
只有当我将其发布到网站时,才会发生这种情况。当我在调试模式下运行它时,一切都正常运行。另外,我还有其他与之完全相同的ajax帖子,这些帖子都可以正常工作。
当我删除contentType:application/json和dataType:json时,我会得到一个空引用异常。我已经检查了很多次,它正在捕获表单中的所有字段。您缺少显示javascript变量myPlayer是什么的代码请参见[SO post][1]。[1] :谢谢ps2Goat,我错过了添加。我现在已经添加了它。基本上,它只是一个var myPlayer=$editForm.serialize;谢谢你的链接,文卡特。我试过那张单子上的每一样东西,但都不走运。问题是,我的代码在调试模式下运行平稳。当我把它发布到网站上时,它给了我那个错误。我有多个操作,我用Ajax调用了大部分(如果不是全部的话)操作,它们的编码方式基本相同。其他的都能用。
[ArgumentException: Invalid JSON primitive: Category.]
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject() +745
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth) +362
System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer) +115
System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit) +49
System.Web.Mvc.JsonValueProviderFactory.GetValueProvider(ControllerContext controllerContext) +242
System.Web.Mvc.<>c__DisplayClassc.<GetValueProvider>b__7(ValueProviderFactory factory) +28
System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +248
System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +165
System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +536
System.Linq.Enumerable.ToList(IEnumerable`1 source) +80
System.Web.Mvc.ValueProviderFactoryCollection.GetValueProvider(ControllerContext controllerContext) +347
System.Web.Mvc.ControllerBase.get_ValueProvider() +66
System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +81
System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +153
System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__1e(AsyncCallback asyncCallback, Object asyncState) +955
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +146
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +166
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag) +27
System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__17(AsyncCallback asyncCallback, Object asyncState) +50
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +146
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +166
System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +497
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +146
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +166
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag) +27
System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +409
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__2(AsyncCallback asyncCallback, Object asyncState) +144
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +146
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +166
System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag) +27
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +364
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +12650919
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288