模型绑定不labelfor工作 [英] Model binding not working with labelfor

查看:85
本文介绍了模型绑定不labelfor工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的模型:

    public string CustomerNumber { get; set; }
    public string ShipMethod { get; set; }
    public string ContactPerson { get; set; }
    public string ShipToName { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string Address3{ get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }

我的观点在这里的部分​​:

Here part of my view:

 <table class="table table-condensed">
                <thead>
                    <tr>
                        <td>Customer Number</td>
                        <td>Ship Method</td>
                        <td>Contact Person</td>
                        <td>Ship to Name</td>
                        <td>Address 1</td>
                        <td>Address 2</td>
                        <td>Address 3</td>
                        <td>City</td>
                        <td>State</td>
                        <td>Zip</td>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>@Html.LabelFor(x => x.CustomerNumber, Model.CustomerNumber)</td>
                        <td>@Html.LabelFor(x => x.ShipMethod, Model.ShipMethod)</td>
                        <td>@Html.LabelFor(x => x.ContactPerson, Model.ContactPerson)</td>
                        <td>@Html.LabelFor(x => x.ShipToName, Model.ShipToName)</td>
                        <td>@Html.LabelFor(x => x.Address1, Model.Address1)</td>
                        <td>@Html.LabelFor(x => x.Address2, Model.Address2)</td>
                        <td>@Html.LabelFor(x => x.Address3, Model.Address3)</td>
                        <td>@Html.LabelFor(x => x.City, Model.City)</td>
                        <td>@Html.LabelFor(x => x.State, Model.State)</td>
                        <td>@Html.LabelFor(x => x.ZipCode, Model.ZipCode)</td>
                    </tr>
                </tbody>
            </table>

中的数据被正确地显示在视图中,但当发生后我的网页上,数据回来空。我需要在那些LabelFors数据被发送回我的控制器,所以我没有将它存储在会话中。我觉得MVC应该与labelfors自动的绑定到你的模型。我在做什么错了?

The data gets displayed in the view correctly, but when a post happens on my page, the data comes back null. I need the data in those LabelFors to be sent back to my controller, so I dont have to store it in a session. I thought MVC was supposed to bind to your model automagically with the labelfors. What am I doing wrong?

修改

我猜我问这个问题的原因,是因为我刚刚从web表单搬到MVC和我pretty没有视图状态丢失。我想如果在我的模型的值保持从视图发布还给我,我就不会去我的模型存储在一个会话对象。在我的网页,我需要能够在页面周期要坚持我的模型,这样我就可以在用户单击保存按钮后,我的模型数据存储到一些SQL表。什么是一些选项来坚持MVC模型?

I guess the reason I asked this question, is because I just moved from webforms to MVC, and I am pretty lost without the viewstate. I figured if the values in my model kept posting back to me from the view, I wouldn't have to store my model in a session object. On my page, I need to be able to persist my model during page cycles, so I can store my model data into some sql tables after the user clicks the save button. What are some options to persist your model in MVC?

推荐答案

它只是结合输入元素的表单里(因为浏览这些帖子)。标签的不POST'ed。

It only binds input elements inside a form (because the browser posts these). Label's aren't POST'ed.

您可以使用 HiddenFor ..因为它们是输入元素:

You can use HiddenFor.. since they are input elements:

@Html.HiddenFor(x => x.City)

...等。你就不能使用标签发送回控制器。

..etc. You just can't use labels for sending back to the Controller.

LabelFor

<label></label> <!-- Not POST'ed by the browser -->

HiddenFor

<input type="hidden" /> <!-- POST'ed by the browser -->

这篇关于模型绑定不labelfor工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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