Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6695637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:14:45+00:00 2026-05-26T06:14:45+00:00

I have a problem with a TryUpdateModel in MVC3 When the Edit (post) is

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:14:45+00:00Added an answer on May 26, 2026 at 6:14 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem with following code. mySpriteArray=[[NSMutableArray alloc] init]; star=[CCSprite spriteWithFile:@22.png]; for(int i=0;i<10; i++)
I have problem with xhr open() method. My code follows : var xmlhttp=false; if(!xmlhttp)
I have problem with validation such code function show_help_tip(event, element) { var $e =
I have an [HttpPost] action method signature like this: [HttpPost] public ActionResult Edit(ExistingPostViewModel model)
I have the following classes: public class ComplexType { public long? Code { get;
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have problem with databinding in the Win.Forms DataGridView control. Example: public class A
I have jqGrid with enabled EDIT, DELETE, ADD and VIEW , Now my problem
I have the following on my edit screen: <label for=campaign.CandidateID>Candidate:</label> <%= Html.DropDownList(Campaign.CandidateID, Model.Candidates, Choose...)%>
Say I have a class, e.g.: public class Person { public virtual int Id

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.