0

我有: 查看

<script type="text/javascript">
function postData() {
    var urlact = '@Url.Action("createDoc")';
    var model = '@Html.Raw(Json.Encode(Model))';

    alert(model);

    $.ajax({
        data: stringify(model),
        type: "POST",
        url: urlact,
        datatype: "json",
        contentType: "application/json; charset=utf-8",
        success: function (result) {
            window.location.href = '@Url.Action("CreateWordprocessingDocument","Movies")'
        }
    });
}
</script>

控制器

[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; }
}

为什么当我将字符串化数据从视图(通过 Ajax 发布函数)发布到控制器(createDoc)时,它停止抛出 ArgumentNullException(似乎传递的模型为空)?任何解决方法/解决方案?

注意:没有对模型进行字符串化,一切正常,但我试图对其进行字符串化,否则我在 DateTime 格式方面遇到了一些问题。

注意/2:我也尝试将Controller 操作的输入参数中的字符串 mov替换为IEnumerable movies,但它也不起作用。

4

2 回答 2

0

添加评论@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);
            }
    }

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);
    }

您可以使用不同的转换器

于 2012-06-12T16:26:57.603 回答
0

您的模型已经是 JSON 编码的,因此您不需要对其进行字符串化。这可能导致无效的 Json 数据,因此它没有被解码。

如果您的 DateTime 格式是问题,请解释该问题,以便我们帮助解决它。

于 2012-06-11T14:16:36.600 回答