试验是在后期控制器空 [英] test is null in the controller upon post

查看:115
本文介绍了试验是在后期控制器空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的codeS和测试值总是在后门柱控制器空。有什么不对下面code:

型号:

 公共类手提箱
{
    公共串色{搞定;组; }
    公共字符串[] {尺寸得到;组; }
    公开名单<串GT;服装{搞定;组; }
    公开名单<试验>测试{搞定;组; }
}
公共类测试
{
    公共字符串名称{;组; }
    公众诠释ID {搞定;组; }
}

视图:

 <&字段集GT;
        <传奇>所有关于我的行李< /传说>        < D​​IV CLASS =编辑标记>
            <%:Html.LabelFor(型号=> model.Color)%GT;
        < / DIV>
        < D​​IV CLASS =主编场>
            <%:Html.TextBoxFor(型号=> model.Color)%GT;
        < / DIV>
        < BR />        < D​​IV CLASS =编辑标记>
            宽度,高度,深度:
        < / DIV>
        < D​​IV CLASS =主编场>
           ml.TextBoxFor(型号=> model.Depth,新{风格=宽度:50像素;})%GT;
            < / DIV>
        < BR />        < D​​IV CLASS =编辑标记>旅行箱内容与LT; / DIV>
        < D​​IV CLASS =主编场>            < D​​IV ID =衣服的编辑器>
                服装项目:其中;输入类型=文本ID =新服项目的风格=宽度:150像素/> <按钮的ID =加衣>添加到手提箱< /按钮>
            < / DIV>            < B个项目,目前手提箱:LT; / B>
            < UL ID =衣清单>
            < / UL>        < / DIV>
        &所述p为H.;
            <按钮ID =包 - 它>放在行李Carosel< /按钮>
        &所述; / P>    < /字段集>    <脚本类型=文/ JavaScript的LANGUAGE =JavaScript的>
        $(函数(){
            $(按钮)按钮()。            //允许用户添加项目到手提箱
            $(#加衣)。点击(函数(){
                VAR clothesText = $(#新服项目);
                $(#衣清单)追加(<立GT;+ clothesText.val()+< /李>);
                clothesText.val()专注()。
            });            //收拾行李箱起来,并将其发送到行李carosel ...呃...控制器
            $(#包 - 它)。点击(函数(){                变种clothesList = [];                $(#衣服名单里)。每个(函数(){
                    clothesList.push($(本)的.text())
                });                变种SizeList的= [];
                SizeList的[0] =中等;
                SizeList的[1] =大;
                SizeList的[2] =小;                VAR数据=新的对象();
                Data.test = [];                VAR读数= {};
                reading.Name =迈克尔
                reading.ID = 123;
                Data.test [0] =读数;                reading.Name =罗文
                reading.ID = 1234;
                Data.test [1] =读数;                $阿贾克斯({
                    输入:POST,
                    传统:真实,
                    数据:{
                        颜色:$(#颜色)VAL()。
                        尺寸:SizeList的,
                        衣服:clothesList,
                        测试:Data.test
                    }
                });
            });
        });
    < / SCRIPT>

控制器:

  [HttpPost]
    公共EmptyResult手提箱(旅行箱lookWhatIPacked)
    {
        返回新EmptyResult();
    }


解决方案

它可能没有关系,但我不认为这code是做你打算什么:

  VAR读数= {};
reading.Name =迈克尔
reading.ID = 123;
Data.test [0] =读数;reading.Name =罗文
reading.ID = 1234;
Data.test [1] =读数;

这是加入同一个对象 Data.test 两次,因为你没有设置阅读是一个新的数组,所以你要更新原来的对象有罗文为名称和1234作为 ID

I have the following codes and the test value is always null in the controller after the post. What is wrong with the following code:

Model:

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

The view:

<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>

Controller:

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

解决方案

It's probably not related, but I don't think this code is doing what you intend:

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

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

This is adding the same object to Data.test twice, as you don't set reading to be a new array, so you're updating the original object to have "Rowen" as the Name and 1234 as the ID.

这篇关于试验是在后期控制器空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆