0

So I have an edit page for bios

[Route("Bio/BioEdit/{USER_ID}/{BIO_SEQ}")]
public ActionResult BioEdit(Int32 USER_ID, Int16 BIO_SEQ)
{
    var bioRecord = db.BIOS.Where(x => x.USER_ID == DMCID && x.BIO_SEQ == BIO_SEQ).FirstOrDefault();
    return View(bioRecord);
}

This succesfully finds the Bio I want to update, and returns a view with a table and the data from the bio that's editable..

Now I have another action for editing with http Post that looks like

 [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult BioEdit([Bind(Include = "BIO_CONTENT")] DAL.BIOS bio)
        {
            // ??
            return View();
        }

Since I'm getting a bio item from the post, can I use that directly and do something like bio.Update()

or do I need to also Bind the BIO_SEQ and USER_ID fields again so I can do something like

       var bioRecord = db.BIOS
        .Where(x => x.USER_ID == DMCID && x.BIO_SEQ == BIO_SEQ).FirstOrDefault()
        .Update(x => new DAL.BIOS() {
            BIO_CONTENT: bio.BIO_CONTENT
        });
  • 1
    Can you - Yes (but you need to post back and bind all properties of the object i.e. no `[Bind]` attribute). However that is not recommended. The recommended approach is to use a view model, and in the POST method, get the data model, update it from the view model and save - [What is ViewModel in MVC?](https://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  Mar 02 '18 at 21:17
  • To expand Stephens comment, you can create a view model that holds the information you are going to update. Then, in the update action you will find that Bio with the same ID and you can copy new values from the view model to the Bio from the database. – Anis Alibegić Mar 03 '18 at 01:13

0 Answers0