Reputation: 227
i am currently working on a mvc based project.I saw people using models n controllers in different ways,im wondering which one is the correct method according to mvc principles
Method-1:
controller:
public ActionResult index()
{
return View();
}
public string save ()
{
return null;
}
public string Update()
{
return null;
}
model:
public string xx {get;set;}
public string yy {get;set;}
Method-2:
controller:
public ActionResult index()
{
return View();
}
model:
public string xx {get;set;}
public string yy {get;set;}
public string save ()
{
return null;
}
public string Update()
{
return null;
}
Upvotes: 0
Views: 66
Reputation: 2850
I would say neither is correct though method 2 is closer to it.
You should have your logic in a separate service which is consumed by your controllers.
Example:
public ActionResult DoStuff() {
var model = Service.save();
return View(model);
}
public class Model {
public string xx { get; set; }
public string yy { get; set; }
}
public class Service
{
public string save ()
{
return null;
}
public string Update()
{
return null;
}
}
Neither the controllers or the models should have business logic in them. They should only have a single responsibility. The model stores data that is returned by the controller. The controller simply transforms the data it receives from the service into a model.
Upvotes: 2
Reputation: 1127
Method
1.because Save /Update is Action Method
which should be within Controller
and not Model.
Upvotes: 0