开发者

Issue with TryUpdateModel in MVC3

开发者 https://www.devze.com 2023-04-13 03:00 出处:网络
I have a problem with a TryUpdateModel in MVC3 When the Edit (post) is fired, I have the following code:

I have a problem with a TryUpdateModel in MVC3

When the Edit (post) is fired, I have the following code:

    public ActionResult Edit(int id, FormCollection collection)
    {
        var review = FoodDB.FindByID(id);
        if (TryUpdateModel(review))
            return RedirectToAction("Index");
        return View(review);
    }

The view is built directly by the VS (so not changed by me) If I trace the code, I see the new values in FormCollection, but after executing TryUpdateModel, it returns true, doesn't throw any error, but the review object isn't updated.

What could I do wrong?

EDIT

I come up with some more details: First, the db is not real DB, but just a "simulation" - class with one static genric List

List<Review> Review;  

Review class is simply a POCO, as below:

public class Review
{
    public string Message { get; set; }
    public DateTime Created { get; set; }
    public int ID { get; set; }
    public int Rating { get; set; }
}

The view is strong-typed, generated by VS from the Edit method of the controller. Fields are defined as below:

    <div class="editor-label">
        @Html.LabelFor(model => model.Message)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Message)
        @Html.ValidationMessageFor(model => model.Message)
    </div>       

    @Html.HiddenFor(model => model.ID)

    <div class="editor-label">
        @Html.LabelFor(model => model.Rating)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Rating)
        @Html.ValidationMessageFor(model => model.Rating)
    </div>

Call to var review = FoodDB.FindByID(id); returns Review object

Even if TryUpdateModel(review) does not work (I trace through code, and I inspected review object before and after 开发者_如何转开发the call, as well as the collection, and it receives correct values), yet the review obj is not updated.

However, I replaced it with my own hand-written method, as below, and in this case the review object DOES get updated:

    private void MyTryUpdateModel(Review review, FormCollection collection)
    {
        review.Message = collection["Message"];
        review.Rating = int.Parse(collection["Rating"]); 
    }   

So the TryUpdateMethod SHOULD find proper fields in collection for updating, as I understand.

So, what can be wrong?

Thanks all


Based on the code you posted, the review object is not updated, because the new values in FormCollection have not been bound to your model. You are not using the DefaultModelBinder.

If your view is strongly typed (and assuming the type class is named Food), change your method signature and method as follows:

public ActionResult Edit(Food food)
{
    if (ModelState.IsValid)
    {
        FoodDB.Update(food);
        return RedirectToAction("Index");
    }
    return View(food);
}

The DefaultModelBinder will take the values from the form and bind them to your model.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号