发布时,模型的属性变为null

时间:2015-06-06 16:50:14

标签: asp.net-mvc-4

我是MVC的新手。现在我正在尝试使用MVC的简单演示来打印客户的信息以进行筛选和更新,然后发送回数据库。

我不知道为什么客户的ID变为空而其他客户的ID没有问题。

我想在屏幕上显示客户ID,但我不希望用户编辑它以发布到我的数据库。我已经研究了几个小时了..

我的代码:

Customer.cs

#include "displaywidget.h"

DisplayWidget::DisplayWidget(int width, int height, QWidget *parent): QWidget(parent)
{
    //ui.setupUi(this);

    w = width;
    h = height;

    CreateWidgets();
    SetupGui();

    // seed the random number generator
    srand(RAND_SEED);
    GenerateNewData();
}

DisplayWidget::~DisplayWidget()
{
}

void DisplayWidget::CreateWidgets()
{
    bImage = new QImage(w, h, QImage::Format_ARGB32);
    dImage = new QImage(w, h, QImage::Format_ARGB32);
    bimageLabel = new QLabel(this);
    dimageLabel = new QLabel(this);

    debugButton = new QPushButton("DEBUG", this);
    bimageLabel->setStyleSheet("QLabel {background-color: black};");
    dimageLabel->setStyleSheet("QLabel {background-color: white};");

    frameGrab = new QTimer(this);
}

void DisplayWidget::SetupGui()
{
    QHBoxLayout * layout = new QHBoxLayout();
    setLayout(layout); // commenting this line out makes it refresh
    layout->addWidget(bimageLabel);
    layout->addWidget(dimageLabel);
    layout->addWidget(debugButton);

    connect(frameGrab, SIGNAL(timeout()),this, SLOT(GenerateNewData()));
    connect(debugButton, SIGNAL(clicked()), this, SLOT(GenerateNewData()));
    frameGrab->start(50);
}

void DisplayWidget::GenerateNewData()
{
    QRgb * bImageData = (QRgb *)bImage->scanLine(0);
    QRgb * dImageData = (QRgb *)dImage->scanLine(0);

    for (int i; i < w * h; i++)
    {
        bImageData[i] = qRgba(rand() % FULLSCALE, 0, 0, FULLSCALE);
        dImageData[i] = qRgba(0, 0, rand() % FULLSCALE, FULLSCALE);
    }

    bimageLabel->setPixmap(QPixmap::fromImage(*bImage).scaled(QSize(IM_PIXEL_WIDTH, IM_PIXEL_HEIGHT)));
    dimageLabel->setPixmap(QPixmap::fromImage(*dImage).scaled(QSize(IM_PIXEL_WIDTH, IM_PIXEL_HEIGHT)));

    //this->update(); // this breaks it again
}

Index.cshtml

Qt

HomeController.cs

public class MainActivity extends Activity {

    private AutoCompleteTextView et_search_game;
    private TextView TextViewForAutoComplete;
    private static String[] GAMES_NAME = new String[] { "blue", "red", "black" };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //AutoCompleteTextView Setting.
        et_search_game = (AutoCompleteTextView)findViewById(R.id.actv_search_game_name_xml);

        //TextView Setting.
        TextViewForAutoComplete = (TextView)findViewById(R.id.tv_for_auto_complete_tv_xml);

        ArrayAdapter<String> arrayAdapterForGamesName = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, GAMES_NAME);

        et_search_game.setThreshold(1);
        et_search_game.setAdapter(arrayAdapterForGamesName);

        et_search_game.setOnItemClickListener(new OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> a, View v, int i, long l) {
                // TODO Auto-generated method stub
                if(et_search_game.getText().toString().trim()=="blue"){
                    setContentView(R.xml.blue);
                } else if (et_search_game.getText().toString().trim()=="red"){
                    setContentView(R.xml.red);
                } else if (et_search_game.getText().toString().trim()=="black") {
                    setContentView(R.xml.black);
                }
            }
        });
    }

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

在MVC中,如果你想从视图中获取一些值,你必须先在视图中获得该值。您似乎没有在视图中输入ID。

更改您的观看代码,

for (var i = 0; i < Model.Count; i++)
            {
                <tr>
                    <td>
                        @Html.HiddenFor(model => model[i].Id)
                    </td>
                    <td>
                        @Html.TextBoxFor(model => model[i].Name, new { @style = "min-width:100%; text-align:center", @disable = "true" })
                    </td>
                    <td>
                        @Html.TextBoxFor(model => model[i].Age)
                    </td>
                </tr>
            }

希望这会有所帮助!!

答案 1 :(得分:0)

实际上,您没有在index.html视图中显示ID,客户ID在您的示例中具有值。您应该尝试在for each循环中以只读模式显示它。

@Html.DisplayFor(model => model[i].Id)