【问题标题】:Post ajax data to ASP.NET MVC Controller将 ajax 数据发布到 ASP.NET MVC 控制器
【发布时间】:2014-06-17 03:26:34
【问题描述】:

我在向 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”参数为空。 ajax post调用(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 帖子:

控制器中的断点,ArchivosDeNormas 属性为空:

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

【问题讨论】:

  • 在公共类ArchivosDeNormaC中,将public int id改为public string id。

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


【解决方案1】:

您将 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;
    };

【讨论】:

  • 非常感谢 arsebin3!完美运行!
【解决方案2】:

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

【讨论】:

  • 其实它确实会做复杂的对象,很简单。
【解决方案3】:

您不能将 Form.Serialize 与 Json 混合使用 application/x-www-form-urlencoded。它要么 ALL 必须是 Form Querystring 要么 ALL json。

换句话说:

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

未正确 urlencoded (application/x-www-form-urlencoded)。

【讨论】:

    猜你喜欢
    • 2016-02-07
    • 2013-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多