简体   繁体   中英

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. I thought MVC was supposed to bind to your model automagically with the 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. 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?

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:

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

MVC will work according to HTML standards which means it won't postback a label element.

Use a HiddenFor or TextboxFor with a readonly attribute.

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

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]
public ViewResult Edit(Customer customer) {
    if (ModelState.IsValid) {
        // Save your customer
        return RedirectToAction("Index");
    }
    else {
        return View(customer);
    }
}

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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