English 中文(简体)
与 Json 收藏序列的 MVC3 表格为空, 与 Ajax Post
原标题:MVC3 form serialized with JSon collection is empty with Ajax Post

我试图用一个收藏物体的属性传递一个对象,但当使用序列表作为我的 Ajax 日志中的数据属性时,该收藏是空的(null ) 。 这是否是错误的方法来传递一个包含作为属性列表的物件或者我所忽略的简单的东西? 当我进入控制器动作时, 我可以看到我找到了 Holder. Id 属性, 但是 MyList 是空的 。

我尝试过制作内容Type: 应用程序/json, 没有任何帮助。 我希望将完整对象作为 Ajax 哨所的有效载荷, 而不是收藏中的每个单个物品。 在给 Ajax 打电话时, 每件物品都有效, 我可能不得不使用这种方法 。

简单的 POCO 对象 :

public class Holder
{
    public int Id { get; set; }
    public List<Contained> MyList { get; set; }
}

public class Contained
{
    public int Id { get; set; }
    public string Name { get; set; }
}

主计长方法:

    [HttpGet]
    public ActionResult Edit()
    {
        List<Contained> contained = new List<Contained>();
        contained.Add(new Contained{Id=123, Name="123Name"});
        contained.Add(new Contained{Id=456, Name="456Name"});
        return View(new Holder { Id = 1001, MyList = contained });

    }

    [HttpPost]
    public JsonResult Edit(Holder holder)
    {
        return Json("Succeeded");
    }

Html 标记 :

@model Compeat.MyMvcApp.Controllers.Holder
@{
     ViewBag.Title = "Edit";
}
<script type="text/javascript">
$(document).ready(function () {
    $("#btnAdd").click(function () {
        var coll = [];
        var newName = $("#txtAddNameToList").val();
        coll.push(newContainedJson(999, newName));
        $("#MyList").val(coll);
        var frmElems = $("form").serialize();
        console.log(coll);
        $.ajax({
            type: "POST",
            data: coll,
            url: "Edit",
            dataType: "json",
            success: function (data) { alert( worked! ); },
            error: function (data) {
                alert( error:   + data.responseText);
            }
        });
    });

    function newContainedJson(id, name) {
        return { "Id": id , "Name": name };
    }
});
</script>
<ul>
@foreach (var o in Model.MyList)
{
    <li>@o.Name</li>
}
</ul>
@{ using (Html.BeginForm())
   {
@Html.HiddenFor(e => e.Id)
<input type="hidden" name="MyList" id="MyList" value="" />
<input type="text" id="txtAddNameToList" name="txtAddNameToList" />
<input type="button" id="btnAdd" value="Add Name" />
}}
最佳回答

使用下面的 J 查询并在您的控制器中设置断点 。

 $("#btnAdd").click(function () {



        var data = {}
        data.Id = 9;
        data.MyList = [];
        data.MyList.push({  Id : 99,  Name :  Dude  });
        data.MyList.push({  Id : 990,  Name :  Dude2  });

        $.ajax({
            type: "POST",
            data: JSON.stringify(data),
            url: "Home/Index",
            dataType: "json",
            contentType:  application/json ,
            success: function (data) { alert( worked! ); },
            error: function (data) {

            }
        });
    });
问题回答

暂无回答




相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...

热门标签