Skip to content

Instantly share code, notes, and snippets.

@rionmonster
Last active April 6, 2016 19:33
Show Gist options
  • Save rionmonster/4c2c644341b32ce9b15856bb63de851c to your computer and use it in GitHub Desktop.
Save rionmonster/4c2c644341b32ce9b15856bb63de851c to your computer and use it in GitHub Desktop.
public class Complaint
{
public int Id { get; set; }
public string Reason { get; set; }
public ComplaintFiler ComplaintFiler { get; set; }
}
public ActionResult Update(Complaint model)
{
// Both the Reason and Id attributes should have been posted
// and will be present on the model
// So now you can actually update your model (for example purposes
// I'm doing this in the Controller, but you might have a more
// abstracted approach (e.g. a repository, etc.)
// Grab your entity
var original = _context.Complaints.Include(c => c.ComplaintFiler)
.FirstOrDefault(c => c.COMP_ID == model.Id);
if(original != null)
{
// We found your original complaint, so update it
original.ComplaintReason = model.Reason;
// If you wanted to update a related entity or child property that was included, you could do that here
original.ComplaintFiler.Name = "Stupid complainer";
// You would update other properties here as necessary
// After all of your changes were made, you would save them
SaveAll();
return original;
}
// Your entity wasn't found, handle accordingly
return HttpNotFound();
}
@model Complaint
<form action='@Url("Update","Home")'>
@Html.TextBoxFor(m => m.Reason)
@Html.HiddenFor(m => m.Id)
<input type='submit' value='Update!' />
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment