在post上,控制器中的test为null

时间:2010-06-14 11:19:16

标签: asp.net-mvc

我有以下代码,并且在帖子后控制器中的测试值始终为null。以下代码有什么问题:

型号:

public class Suitcase
{
    public string Color { get; set; }
    public string[] Size { get; set; }
    public List<string> Clothes { get; set; }
    public List<Test> test { get; set; }
}
public class Test
{
    public string Name { get; set; }
    public int ID { get; set; }
}

观点:

<fieldset>
        <legend>All about my baggage</legend>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.Color) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.Color) %>
        </div>
        <br />

        <div class="editor-label">
            Width, Height, Depth:
        </div>
        <div class="editor-field">
           ml.TextBoxFor(model => model.Depth, new { style = "width:50px;" })%>
            </div>
        <br />

        <div class="editor-label">Suitcase Contents</div>
        <div class="editor-field">

            <div id="clothes-editor">
                Clothing Item: <input type="text" id="new-clothes-item" style="width:150px" /> <button id="add-clothes">Add to suitcase</button>                
            </div>

            <b>Items currently in suitcase:</b>
            <ul id="clothes-list">                    
            </ul>

        </div>
        <p>
            <button id="pack-it">Put on Baggage Carosel</button>
        </p>



    </fieldset>

    <script type="text/javascript" language="javascript">
        $(function () {
            $("button").button();

            // allow users to add items to the suitcase
            $("#add-clothes").click(function () {
                var clothesText = $("#new-clothes-item");
                $("#clothes-list").append("<li>" + clothesText.val() + "</li>");
                clothesText.val("").focus();
            });

            // pack the suitcase up and send it to the baggage carosel...erm...controller
            $("#pack-it").click(function () {

                var clothesList = [];

                $("#clothes-list li").each(function () {
                    clothesList.push($(this).text())
                });

                var SizeList = [];
                SizeList[0] = "Medium";
                SizeList[1] = "Large";
                SizeList[2] = "small";

                var Data = new Object();
                Data.test = [];

                var reading = {};
                reading.Name = "Micheal"
                reading.ID = 123;
                Data.test[0] = reading;

                reading.Name = "Rowen"
                reading.ID = 1234;
                Data.test[1] = reading;



                $.ajax({
                    type: 'POST',
                    traditional: true,
                    data: {
                        Color: $("#Color").val(),
                        Size: SizeList,
                        Clothes: clothesList,
                        test: Data.test
                    }
                });
            });
        });        
    </script>

控制器:

[HttpPost]
    public EmptyResult Suitcase(Suitcase lookWhatIPacked)
    {
        return new EmptyResult();
    }

1 个答案:

答案 0 :(得分:0)

它可能没有关系,但我认为这段代码不符合您的意图:

var reading = {};
reading.Name = "Micheal"
reading.ID = 123;
Data.test[0] = reading;

reading.Name = "Rowen"
reading.ID = 1234;
Data.test[1] = reading;

这是将同一个对象添加到Data.test两次,因为您没有将reading设置为新数组,因此您要更新原始对象以将“Rowen”作为{ {1}}和1234作为Name