Asp.net mvc MVC3(Razor)Json字符串化数据返回null(ArgumentNullException)
我有: 看法 为什么当我通过Ajax post函数将视图中的字符串化数据发布到Controller createDoc时,它会停止抛出ArgumentNullException,因为传递的模型似乎是空的? 有什么变通方法/解决方案吗 注意:在没有对模型进行字符串化的情况下,这一切都是可行的,但我正试图对它进行字符串化,因为从另一个角度看,我对DateTime格式有一些问题Asp.net mvc MVC3(Razor)Json字符串化数据返回null(ArgumentNullException),asp.net-mvc,json,razor,stringify,Asp.net Mvc,Json,Razor,Stringify,我有: 看法 为什么当我通过Ajax post函数将视图中的字符串化数据发布到Controller createDoc时,它会停止抛出ArgumentNullException,因为传递的模型似乎是空的? 有什么变通方法/解决方案吗 注意:在没有对模型进行字符串化的情况下,这一切都是可行的,但我正试图对它进行字符串化,因为从另一个角度看,我对DateTime格式有一些问题 注意/2:我也尝试过用IEnumerable movies替换控制器操作输入参数中的字符串mov,但也不起作用。您的模型已经
注意/2:我也尝试过用IEnumerable movies替换控制器操作输入参数中的字符串mov,但也不起作用。您的模型已经是JSON编码的,因此不需要对其进行字符串化。这可能导致Json数据无效,因此无法对其进行解码
如果您的日期时间格式是问题所在,请解释该问题,以便我们能够帮助解决。添加到@Jaimal关于JSON.NET配置的回答中。我使用以下方法:
public class JsonNetResult : JsonResult
{
public JsonNetResult()
{
Formatting = Formatting.None;
}
public Encoding ContentEncoding { get; set; }
public string ContentType { get; set; }
public object Data { get; set; }
public JsonSerializerSettings SerializerSettings { get; set; }
public Formatting Formatting { get; set; }
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
throw new ArgumentNullException("context");
var response = context.HttpContext.Response;
response.ContentType = !string.IsNullOrEmpty(ContentType) ? ContentType : "application/json";
if (ContentEncoding != null)
response.ContentEncoding = ContentEncoding;
if (Data == null)
return;
// If you need special handling, you can call another form of SerializeObject below
var serializedObject = JsonConvert.SerializeObject(Data, Formatting, new JavaScriptDateTimeConverter());
response.Write(serializedObject);
}
}
与
你可以使用不同的方法 DateTime格式的问题在于,当我从控制器打印传递给控制器自身的日期时,它会为每个日期打印01/01/0001 00:00:00。我无法修改/解析传递的日期,因为我直接在控制器操作中的IEnumerable变量中收到它。如果问题出在JSON反序列化程序中,may JSON.NET会更好。您好,@Napoleoss感谢您的回答。我曾尝试将JSON.NET与许多基于web的示例一起使用,但我无法理解。您能否在json.net中发布问题及其解决方案的示例代码?对不起打扰你了,谢谢。
[HttpPost]
public void createDoc(string mov)
{
var movies = new JavaScriptSerializer().Deserialize<IEnumerable<Movie>>(mov);
//using movies...
}
//[Serializable]
public class Movie
{
//Added Data Annotations for Title & Genre
public int ID { get; set; }
[Required(ErrorMessage = "Insert name")]
public string Title { get; set; }
[DataType(DataType.Date)]
public DateTime ReleaseDate { get; set; }
[Required(ErrorMessage = "Inserist genre")]
public string Genre { get; set; }
[DataType(DataType.Currency)]
public decimal Price { get; set; }
}
public class JsonNetResult : JsonResult
{
public JsonNetResult()
{
Formatting = Formatting.None;
}
public Encoding ContentEncoding { get; set; }
public string ContentType { get; set; }
public object Data { get; set; }
public JsonSerializerSettings SerializerSettings { get; set; }
public Formatting Formatting { get; set; }
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
throw new ArgumentNullException("context");
var response = context.HttpContext.Response;
response.ContentType = !string.IsNullOrEmpty(ContentType) ? ContentType : "application/json";
if (ContentEncoding != null)
response.ContentEncoding = ContentEncoding;
if (Data == null)
return;
// If you need special handling, you can call another form of SerializeObject below
var serializedObject = JsonConvert.SerializeObject(Data, Formatting, new JavaScriptDateTimeConverter());
response.Write(serializedObject);
}
}
public abstract class BaseController : Controller
{
/// <summary>
/// Use JSON.NET
/// </summary>
protected override JsonResult Json(object data, string contentType, Encoding contentEncoding)
{
var result = new JsonNetResult
{
Data = data,
ContentType = contentType,
ContentEncoding = contentEncoding,
Formatting = Formatting.Indented
};
return result;
}
}
public JsonResult Get()
{
return Json(myModel, null, null);
}