将ajax数据发布到ASP.NET MVC Controller

时间:2014-06-16 15:18:51

标签: c# jquery ajax asp.net-mvc

我在向MVC控制器发送数据时遇到问题...

我的模特(请参阅酒店“ArchivosDeNorma”):

public partial class CustomNormas
{
    #region Propiedades

    [Required(ErrorMessage = "Debe ingresar Titulo")]
    [StringLength(50, ErrorMessage ="Titulo no puede superar los 50 caracteres")]
    public string Titulo { get; set; }

    [Required(ErrorMessage = "Debe ingresar FechaDeVigencia")]
    public DateTime FechaDeVigencia { get; set; }

    public DateTime? FechaDePublicacion { get; set; }

    [Required(ErrorMessage = "Debe ingresar Ambito")]
    public int Ambito { get; set; }

    [Required(ErrorMessage = "Debe ingresar Tipo")]
    public int Tipo { get; set; }

    [Required(ErrorMessage = "Debe ingresar Numero")]
    public int Numero { get; set; }

    [Required(ErrorMessage = "Debe ingresar Anio")]
    public int Anio { get; set; }

    [StringLength(300, ErrorMessage ="Descripcion no puede superar los 300 caracteres")]
    public string Descripcion { get; set; }

    [Required(ErrorMessage = "Debe ingresar FechaAlta")]
    public DateTime FechaAlta { get; set; }

    public DateTime? FechaBaja { get; set; }

    [StringLength(2147483647, ErrorMessage ="Texto no puede superar los 2147483647 caracteres")]
    public string Texto { get; set; }

    [StringLength(500, ErrorMessage ="Firmantes no puede superar los 500 caracteres")]
    public string Firmantes { get; set; }

    **public ArchivosDeNormaC[] ArchivosDeNorma { get; set; }**

    public string DescripcionArchivo { get; set; }

    #endregion

}

**public class ArchivosDeNormaC
{
    public int id { get; set; }
    public string archivo { get; set; }
    public string descripcion { get; set; }
}**

问题是当ajax发布到控制器时,“ArchivosDeNormas”参数为null。  ajax post call(aarchivos是一个数组):

 var data = v.form.serializeArray();
 data.push({ name: "ArchivosDeNorma", value: JSON.stringify(that.aarchivos) });
 $.ajax({
type: "POST",
url: "Normas/Crear"
data: data
 });

在firebug中发表Ajax帖子:

enter image description here

在控制器中的breackpoint,属性ArchivosDeNormas是空的:

enter image description here

希望有人可以帮助我!非常感谢,对不起我的英语

3 个答案:

答案 0 :(得分:2)

您正在将您的JSON数据提交为application/x-www-form-urlencoded,而应将其作为实际JSON提交:

application/json

这可以非常简单地完成,但.serializeArray();方法不会为原始JSON对象正确地解包。相反,您可以使用下面的.serializeObject()方法:

Javascript修复:

  • 将ContentType设置为JSON
  • 使用serializeObject()
  • 正确创建JSON对象

所有完成如下:

<强> jsFiddle Demo

var data = v.form.serializeObject();
data.ArchivosDeNorma = that.aarchivos;
$.ajax({
    type: "POST",
    contentType: 'application/json',
    url: "Normas/Crear",
    data: JSON.stringify(data)
});

jQuery.fn.serializeObject = function () {
        var arrayData, objectData;
        arrayData = this.serializeArray();
        objectData = {};

        $.each(arrayData, function () {
            var value;

            if (this.value != null) {
                value = this.value;
            } else {
                value = '';
            }

            if (objectData[this.name] != null) {
                if (!objectData[this.name].push) {
                    objectData[this.name] = [objectData[this.name]];
                }

                objectData[this.name].push(value);
            } else {
                objectData[this.name] = value;
            }
        });

        return objectData;
    };

答案 1 :(得分:0)

该值为null,因为您尝试使用默认模型绑定器绑定复杂的子对象。默认模型绑定器不知道如何执行此操作。您可以做的一件事是编写自定义模型绑定器,或者您可以更改要发布回服务器的内容。由于您只需要发送回的ID,因此您只需将一组int发送回服务器即可。 Asp.net mvc非常乐意将其绑定。

答案 2 :(得分:0)

您不能将Form.Serialize与{J}混合使用application/x-www-form-urlencoded。它必须是ALL Querystring或ALL json。

换句话说:

[{"archivo" : "DSC03907.JPG", "id" : "46", "descripcion" : "wqedas" }]

没有正确地进行urlencoded(application/x-www-form-urlencoded)。