I have a ASP.NET MVC application where in all my Models have an Errors Property used to store non-validation errors which I than display in my Views. Where is the right place to populate the error list according to MVC? Inside the model or inside the controller?
First Option
public class MyModel
{
public DataOperationResult DoSomething()
{
//do something with data
if (result != DataOperation.Success)
{
Errors.Add("Something went wrong");
}
return result;
}
}
Second option
public class MyController
{
public ActionResult DoSomething()
{
MyModel model = new myModel();
DataOperationResult result = model.DoSomething();
if (result != DataOperation.Success)
{
model.Errors.Add("Something went wrong");
}
}
}
2
I don`t think storing error in models is good idea. Models are responsible for storing data not data and error. You breaking single responsibility principle. You should store erroros in ViewModels and populate them in controller.