0

"CODE 01: Data update form does the validation correctly but it does not update the records in the table. "

"There's similar code which I have used as an example, which is "CODE 02" that code work but the other does not .... "

"I did accordingly to the CODE 02, but CODE 01 does not work"

@* CODE 01 *@

 public ActionResult Edit(Film F)
        {

            if (ModelState.IsValid) {
                var item3 = db.Films.Where(x => x.Id == F.Id).First();
                item3.Title = F.Title;
                item3.Director = F.Director;
                item3.Actor = F.Actor;
                item3.Year = F.Year;
                item3.Budget = F.Budget;
                ModelState.Clear();
                db.SaveChanges();
                ViewBag.alert = "Your data is updated";
            }
                return View(F);
        }

@* CODE 02 *@

public ActionResult Edit(Film E) {

            var item3 = db.Films.Where(e=> e.Id==E.Id).First();
            item3.Title = E.Title;
            item3.Director = E.Director;
            ViewBag.alert = "Data is success";
            db.SaveChanges();
            return View();
        }

CODE 01 doesnt update the data but CODE 02 does upadate it

Rahul Sharma
  • 7,768
  • 2
  • 28
  • 54
silvachathura
  • 163
  • 1
  • 9

1 Answers1

1

It seems as though you are not fulling defining the "Film" class that you are passing to the edit function. If your model is not being defined as valid then that means that you have something in your class that is either defined wrong or undefined.

An example of this may be you have a budget that you have submitted as a double (2000.30) when your class defines this as an integer or a title or actor is not defined when it is not of a nullable type OR you have data annotations in your class restricting it further and those criteria are not being met.

refer to ModelState.IsValid question here