【发布时间】:2014-09-23 20:52:51
【问题描述】:
我目前正在做一个基于 mvc 的项目。我看到人们以不同的方式使用模型 n 控制器,我想知道根据 mvc 原理哪一个是正确的方法
方法一:
控制器:
public ActionResult index()
{
return View();
}
public string save ()
{
return null;
}
public string Update()
{
return null;
}
型号:
public string xx {get;set;}
public string yy {get;set;}
方法二:
控制器:
public ActionResult index()
{
return View();
}
型号:
public string xx {get;set;}
public string yy {get;set;}
public string save ()
{
return null;
}
public string Update()
{
return null;
}
【问题讨论】:
-
方法 1. 您的保存/更新操作应该是控制器中的 ActionResults(或类似的),而不是您的模型。在保存/更新时,您将更新您的模型并将其传递回您的视图。
-
模型应该只包含属性?..我应该把数据填充函数放在哪里?在控制器或模型中?
-
您可以从控制器中的操作调用业务逻辑层。