2

好吧,这应该很容易(但显然不适合我)。我有一个 AJAX 例程以 JSON 格式将我的所有数据发送到控制器。一切都很完美,回到控制器的模型对象属性正在正确地填充我从视图发送的所有数据。除了这些属性之一是对象列表,对于它,从视图中,我只发送应该填充列表的对象的 ID。正在发生的事情是,模型对象的 List 属性正在填充仅获取 ID 信息的实例,而我需要它做的是使用从视图发送的 List 中存在的 ID 加载所有其他属性。

一些代码来说明:

        // Creating Documento Json Object
        var docs = { "IDDocumento": "" };

        // Creating Lote Json Object
        var lote = { 
            "IDLote": "", 
            "Numero": "", 
            "Documentos": []
        };

        // Set Lote's Values
        lote.IDLote             = $("#IDLote").val();
        lote.Numero             = $("#Numero").val();

        $.each($('input[name="Documento"]:checked'), function (index, element) {
            var temp = $(element).attr('id').replace('Documento_', '').trim();

            // Set Documento individual Value
            docs.IDDocumento = temp; // << HERE IM FILLING WITH THE DESIRED OBJECT ID

            // adding to Lote.Documentos List Naviagtion Attribute
            lote.Documentos.push(docs);
            docs = { "IDDocumento": "" };
        });

        $.ajax({
            url: '/Lote/Edit',
            data: JSON.stringify(lote),
            type: 'POST',
            contentType: 'application/json;',
            dataType: 'json',
            success: function (result) {
                if (result.Success == "1") {
                    window.location.href = "/Lote/Index";
                }
                else {
                  //Take care of the exception...
                }
            }
        });

上面的视图被强类型化的类是:

public class Lote
{
    [Key]
    public int IDLote { get; set; }

    [Required]
    [MaxLength(20)]
    public string Numero {get;set;}

    public List<Documento> Documentos { get; set; }

最后,导航属性对象类型的类:

public class Documento
{
    [Key]
    public int IDDocumento { get; set; }

    [Required]
    [MaxLength(60)]
    public string Nome { get; set; }

    public List<Lote> Lotes { get; set; }
}

有什么帮助吗?

4

0 回答 0