1

I have this problem of updating records in which it only updates when the original data in the Edit textbox was totally changed with a new value. if the origial value for example is "Apple" and only appended with 'S' to become "Apples" it does not update. it only updates when the entire word will be changed. i used an entity framework to create my data model for the table I created.and used Edit scaffold template for the Edit Form View. Can you help me with this one?

Here's my code in Edit Controller Actions:

public ActionResult Edit(int? Id)
    {
       // if (Id == null)
       // {
           // return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
       // }
        Book student = stud.Books.Find(Id);
        //if (student == null)
        //{
           // return HttpNotFound();
       // }
       return View(student);
    }
    //edit2
    [HttpPost, ActionName("Edit")]
    [ValidateAntiForgeryToken]
    public ActionResult EditPost(int? Id)
    {
        //if (Id == null)
        //{
           //return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        //}
        var studentToUpdate = stud.Books.Find(Id);
        if (TryUpdateModel(studentToUpdate, "",
           new string[] { "BookTitle", "Author"}))
        {
            try
            {
                if (ModelState.IsValid)
                {


                    stud.SaveChanges();

                    return RedirectToAction("Index");
                }
            }
            catch
            {
                //Log the error (uncomment dex variable name and add a line here to write a log.
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
            }
        }
        return View(studentToUpdate);
    }
tintred
  • 11
  • 3
  • See [this question.](http://stackoverflow.com/questions/28904581/how-to-use-tryupdatemodel) It may help out with your case. – parameter Feb 07 '17 at 13:19

0 Answers0