43

I have a controller method CreateOrUpdate, this method is supposed to save the car to the database and then return as normal.

public ActionResult CreateOrUpdate(int ID = 0)
{
    Car car = new Car(ID);
} 

[HttpPost]
public ActionResult CreateOrUpdate(Car car)
{
       car.Save();
       return View(car);
}

In the theCar.Save() method, i set the id for the car, with whatever the id will be in the database after the car is saved (When doing an insert I use SCOPE_IDENTITY(), the save method works well, and if i debug and watch the values for car after the Save() is called, the id is correct. But when the View is rendered the ID is 0 in the view.

Could anyone please help me,and tell me why this would happen. Am I not suppose to change the Model for the view in the HTTP POST method ? Should i Rather redirect to the original CreateOrUpdate() method if the save was successful.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Captain0
  • 2,583
  • 2
  • 28
  • 44
  • try without having `theCar.Save();` line. – Chamika Sandamal Mar 10 '12 at 09:52
  • theCar.Save() only saves the model. This is not the problem. Even if i set a property of the class in the HttpPost method, without the theCar.Save() method. The property is not reflected in the view – Captain0 Mar 10 '12 at 09:57
  • FYI: In the case where you've saved the data successfully and want to redisplay the page might want to follow the Post/Redirect/Get pattern. https://en.wikipedia.org/wiki/Post/Redirect/Get – Brad Boyce Jul 18 '17 at 16:26

3 Answers3

74

it should be the ModelState problem. if you use Htmlhelper to Display id value. Default HtmlHelper display ModelState value not Model. Try display model value in view

<td>
    @Model.id
</td>

or Clean ModelState Value in controller

ModelState.Clear();

or reset id value after SaveChange.

theCar.Save();
ModelState["id"].Value = theCar.id
return View(theCar);

Reset the value of textarea after form submission

Community
  • 1
  • 1
Mason
  • 950
  • 6
  • 11
  • `ModelState["MyModelProperty"].Value = null;` combined with `model.MyModelProperty = null;` (for state maintenance) did the trick for my issue. Thank you for this! – Alexandru Feb 10 '16 at 18:32
  • This has plagued me for the longest time, `ModelState.Clear()` did it for me, thanks! – Novastorm Apr 04 '17 at 09:12
  • 4
    Horrible bug in mvc in my humble opinion – toddmo Apr 21 '17 at 13:46
  • Note that `ModelState.Clear();` will also clear all validation results in ModelState. See Michael's recommendation below if you want to retain the validation results. – tala9999 Oct 09 '19 at 14:36
21

I added ModelState.Clear() to my HttpPost Controller method, as seen in this post Html helpers get data from model state and not from model if you return the same view after form post. to get updated data in the view use post redirect get pattern or ModelState.Clear() and it solved the problem.

Thanks

Community
  • 1
  • 1
Captain0
  • 2,583
  • 2
  • 28
  • 44
4

I didn't want to clear the ModelState because I needed to display errors, so I went with

ValueProviderResult vpr = new ValueProviderResult("", null, System.Globalization.CultureInfo.CurrentCulture);

ModelState["id"].Value = vpr;
MikeT
  • 2,530
  • 25
  • 36