简体   繁体   English

模型绑定不适用于labelfor

[英]Model binding not working with labelfor

Here is my model: 这是我的模型:

    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>

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. 我需要将那些LabelFors中的数据发送回我的控制器,所以我不必将其存储在会话中。 I thought MVC was supposed to bind to your model automagically with the labelfors. 我认为MVC应该通过labelfors自动绑定到您的模型。 What am I doing wrong? 我究竟做错了什么?

EDIT 编辑

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. 我想我问这个问题的原因是因为我只是从Webforms迁移到了MVC,而没有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. 在我的页面上,我需要能够在页面周期内持久保存模型,以便在用户单击“保存”按钮后将模型数据存储到某些sql表中。 What are some options to persist your model in MVC? 在MVC中将模型持久化有哪些选择?

It only binds input elements inside a form (because the browser posts these). 它仅将输入元素绑定在表单内(因为浏览器发布了这些元素)。 Label's aren't POST'ed. 标签未发布。

You can use HiddenFor .. since they are input elements: 您可以使用HiddenFor ..,因为它们是输入元素:

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

..etc. ..等等。 You just can't use labels for sending back to the Controller. 您只是不能使用标签发送回控制器。

LabelFor : LabelFor

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

HiddenFor : HiddenFor

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

MVC will work according to HTML standards which means it won't postback a label element. MVC将按照HTML标准工作,这意味着它不会回发标签元素。

Use a HiddenFor or TextboxFor with a readonly attribute. HiddenForTextboxFor使用具有readonly属性。

But if you just display the values, why not putting them in a session before sending it to the page? 但是,如果仅显示这些值,为什么不将它们发送到页面之前放在会话中呢?

If you have like a wizard style form where you need to save changes inbetween steps before committing the data to a database for example your best bet would be to save the submitted values in a session which you read out again in the next step in the controller. 如果您有向导样式的表单,例如在将数据提交到数据库之前需要在步骤之间保存更改,那么最好的选择是将提交的值保存在会话中,然后在控制器的下一步中再次读取。 Otherwise you can put hidden inputs on your form with the values but that is prone to manipulation by the user. 否则,您可以使用值将隐藏的输入放在表单上,​​但是容易被用户操纵。

Like Simon and others have explained labels are not posted. 像西蒙(Simon)和其他人已经解释过的,标签未发布。 A post request should be used when you want to change something. 要更改某些内容时,应使用发布请求。 You don't need to store it in session if you're just viewing and editing your model. 如果您只是在查看和编辑模型,则无需将其存储在会话中。


In your view, you'll need a link to edit your model: 在您看来,您将需要一个链接来编辑模型:

@Html.ActionLink("edit", "Edit", new {id = Model.CustomerNumber});


In your controller implement your Edit action method: 在您的控制器中实现您的Edit操作方法:

public ViewResult Edit(int customerNumber) {
    var customer = _repository.Customers.FirstOrDefault(c => c.CustomerNumber == customerNumber);
    return View(customer);
}


You'll need a view for the Edit action method and in it goes your form to post your update. 您将需要一个“编辑”操作方法的视图,然后在表单中发布更新。

@using (Html.BeginForm()) {
    <label>Contact Person:</label>
    <input name="ContactPerson" value="@Model.ContactPerson" />
    // the rest of your form

    <input type="submit" value="Save" />
}


Implement the method to handle the post. 实现处理帖子的方法。 Make sure to add the HttpPost attribute. 确保添加HttpPost属性。

[HttpPost]
public ViewResult Edit(Customer customer) {
    if (ModelState.IsValid) {
        // Save your customer
        return RedirectToAction("Index");
    }
    else {
        return View(customer);
    }
}

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM