2

As a beginner with ASP .net MVC i m trying to update database using Entity Framework but the id is always null what it make the modelstate always non Valide this is Code of the Controller

enter image description here

1
  • You need to include the code in the question, not an image of it Commented Feb 7, 2016 at 0:47

2 Answers 2

1

Your Id is equal to null because the model binder doesn't find a value from your posted data.

To solve this you must add the Id as a hidden field into your view. In your Edit.cshtml just add the following line :

@Html.HiddenFor(model => model.Id)
Sign up to request clarification or add additional context in comments.

1 Comment

It is always null :/
0

Try it like this ->

public ActionResult Edit([Bind(Include = "Field1,Field2,Field3...")] Etudiant et)
    {
        if (ModelState.IsValid)
        {
            db.Entry(et).State = EntityState.Modified;

            db.SaveChanges();
        }
        return RedirectToAction("Index");
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.