I'm working on a controller that deals with forms/documents and the more I advance with the tasks the more repeating parts of code I see in my methods. It's my first ASP application at all mvc or not and I'm not sure what is the best way to optimize my code. Something that I've noticed - a pattern that is repeated several time is this :
public ActionResult DisplayForm(int? documentId, long status)
{
ViewBag.Status = status;
List<MCS_DocumentFields> model = (List<MCS_DocumentFields>)DocumentFieldService.GetFieldsForDocument(documentId);
var finalModel = model
.OrderBy(c => c.ContentTypeId)
.ThenBy(c => c.RowNo)
.ThenBy(c => c.ColumnNo)
.ThenBy(c => c.MCS_Fields.Order)
.ToList();
return View(finalModel);
}
This is a method that displays a certain form. But when the form is edited I deal with this in another method :
public ActionResult UpdateDocument(List<MCS_DocumentFields> collection)
{
//TODO deal with the repeating code
int? documentId = (int)collection[0].MCS_Documents.Id;
ViewBag.Status = 1;
List<MCS_DocumentFields> model = (List<MCS_DocumentFields>)DocumentFieldService.GetFieldsForDocument(documentId);
var finalModel = model
.OrderBy(c => c.ContentTypeId)
.ThenBy(c => c.RowNo)
.ThenBy(c => c.ColumnNo)
.ThenBy(c => c.MCS_Fields.Order)
.ToList();
//var ts = collection;
return View("DisplayForm", finalModel);
}
I have to implement the logic for data validation and updating, but at the end I want to show the same view - the edited form with the new data and some proper message like "Save successfull" or something like this.
So I wonder what can I do here - write some private methods in my controller which I'll call where needed. Maybe there's a way to... as in the example - deal with the saving in UpdateDocument method, but then UpdateDocument to return DisplayForm method.. I'm not sure.