How do I update a model in ASP.NET MVC LINQ to SQL from another object?
How do I update a model in ASP.NET using LINQ to SQL?
In Ruby on Rails w开发者_如何学运维e'd do this:
@record1 = Record.find(id) #Get the record
@record1.update_attributes(record2) #Data from record2 is copied to record1
And it generates an SQL update statement.
[HttpPost]
public ActionResult Edit(int id, Record record)
{
//How would I do it in LINQ here?
}
The closest I have come is manually copying all the fields over, that is:
record1.name = record2.name
record1.something = record2.something
Look at the following 2 methods:
http://msdn.microsoft.com/en-us/library/dd470185.aspx
UpdateModel(record)
_service.Save()
And: http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.tryupdatemodel.aspx
if (TryUpdateModel(record))
_service.Save()
TryUpdateModel method is like the UpdateModel method except it does not throw an exception if the updated model state is not valid.
精彩评论