0

I'm working on a project using asp.net MVC with Entity Framework. I was requested to combine the Create page and Edit page together.By another word, if no record , create it, otherwise, edit it. But it brings a problem:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include="ID,Title,ReleaseDate,Genre,Price")] Movie movie)
{
    if (ModelState.IsValid)
    {
        if(movie.ID ==0)
        {
             db.Movies.Add(movie);
        }
        else
        {
             db.Entry(movie).State = EntityState.Modified;
        }
        db.SaveChanges();                
    }
    return View(movie);
}

From above code, you can see that I check the property "id" in DBcontext to know whether it's a new record or existed one. But during test, I found that If I create a record without close the webpage,and change some values, press the "save" button in browser again, a new record will be created instead of modify the record just created.

I have traced the whole workflow and found that:

  1. When I click the Save button first time to create the record, the "id" of movie object does changed after Savechanges function.
  2. When the browser reload the page after creation, the id property of Model in the webpage is really changed to non-zero.
  3. But finally the display result of ID on webpage is still zero~!!! So at the second time, the controller get the value from webpage's form element with 0. 4.No matter I use hiddenfor , hidden, editFor in webpage, the result is the same. 5.Since the validation error message shows on webpage is also changed via post method, I think there maybe some internal settings of EF to prevent value change. But even if I deleted AntiForgery attribute from both controller and webpage side, the result is also the same...

Is there anyone who can help?

Ruyu Bao
  • 23
  • 3
  • store id field as a hidden field in web page when you save first time it will be zero then after saving this record movie model has id changed 0 to any number created by Entity Framework now you are returning the view with this movie model that has id field then render your page with id then no issue of re-creation. – Hitesh Kansagara Mar 31 '18 at 03:23
  • @Hitesh I've really did what you said, but the when I press the save button at the second time, I still get 0 in controller.And I found that not only ID, all other fields can not be changed after post, not matter what `@html.xxxfor` it is.But during trace, the model value in webpage while reloading is really changed... Any other advise? – Ruyu Bao Mar 31 '18 at 03:44
  • But actually when we return from controller the page reloaded and value filled with new model value that is what we have returned and we returned model with Id value then how id will be zero I am also confused – Hitesh Kansagara Mar 31 '18 at 04:11
  • @Hitesh for Get Methods, it's no problem. For Post Methods, the model values did returned to the browser, but it seems the browser will not change the value of '@html.xxxfor' with new value. For MVC, the values passed to Actions via json of the html element in the form, then the second time I can only receive unchanged 0 – Ruyu Bao Mar 31 '18 at 04:32
  • debug your code and see when you returning model from controller after creating record see the movie model it has id filled or not if id filled then debug in view render and see there you get id or not then let me know what happening – Hitesh Kansagara Mar 31 '18 at 04:37
  • If `ModelState` is valid and you insert or update a record, you redirect to another view. The only time you use `return View(...);` in a POST method is if `ModelState` is invalid (and you return the view to display error messages so the user can correct the errors. –  Mar 31 '18 at 04:40
  • And to understand the behavior, refer [this answer](https://stackoverflow.com/questions/26654862/textboxfor-displaying-initial-value-not-the-value-updated-from-code/26664111#26664111) - but using `ModelState.Clear()` is not what you should be doing in this case. –  Mar 31 '18 at 04:41
  • @StephenMuecke Excellent~!!!!!!!!! Thank you very very much ~!! It does work~!!!! Thank you thank you again~!!! – Ruyu Bao Mar 31 '18 at 04:58

0 Answers0