Edit data in entity framework database
I have created a database using entity framework CRUD.It allows editing from the index view only...But i want to edit data with a specific integer type id taken through a textbox..how can i do it??
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Suraj SahooPosted Feb 26, 2015, 2:42 AM
Since you have used CRUD operation, then it edits/updates the record based on the primary key only.
If you want to have a separate edit page to update the record, then you need to have a separate action method for that.
You can also override the edit method. Like
public ActionResult EditRecord(EditModel model)
{
//AddOrUpdate();
//SaveChanges();
}
This will do the operation. You just need to have a Textbox where user will enter the id of the record and a button named Edit. Onclick of that Edit button retrieve the Id entered by the User and then send that to the Action method which will update the record based on the Id.
Remember to the action method you need to pass the whole model properties for the record along with the primary key/id. So that the record will be based on updated values.
You can use Ajax call here like:
var modelData = {
Property1:"",
Property2:"", //so on
};
then assign the properties the values, like
modelData.Property1 = $("#id").val(); //to assign the updated values.
$.ajax({
url:"/CONTROLLER/EDITMETHOD",
type:"POST",
data: modelData,
success: function(){
}
});
I hope this helps.
Accept if helps anyway.
Thanks
Joginder BangerPosted Feb 26, 2015, 2:42 AM
i hope this like might help you
http://www.dotnetcurry.com/showarticle.aspx?ID=619