wali ahmad wali ahmad - 1 month ago 13
ASP.NET (C#) Question

while editing the table with bind, but only some columns error is returned

this is the model:

public class Test
{
[Key]
public int Id{ get; set; }
public string Serial{ get; set; }
public string Name{ get; set; }
}


on the edit with the post

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "Id,Name")] Test test)
{
if (ModelState.IsValid)
{
db.Entry(test).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(test);
}


if you notice there is no Serial bind on edit post so I get ther serial blank or empty

SAR SAR
Answer
[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "Id,Name,Serial")] Test test)
    {
        if (ModelState.IsValid)
        {
            db.Tests.Attach(test); //Tests is your controller i guess
            db.Entry(test).Property(x => x.Name).IsModified = true;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(test);
    }