我使用 MVC 并有一个问题,构建级联 View 模型属性的最佳实践是什么?我可以在两个地方构建连接字段(FullName):
像这样在模型 View 中
public class User { public string FirstName { get; set; } public string LastName { get; set; } public string FullName { get {return FirstName+LastName;} } } public ActionResult Users() { var model = new User { FirstName = "Tomas", LastName = "Saint", }); return View(model); }在 Controller 中
public class User { public string FullName { get; set; } } public ActionResult Users() { var model = new User { FullName = "Tomas" + "Saint"; }); return View(model); }
请您参考如下方法:
如果计算/函数与 View 的格式相关联,那么我会继续将功能放在 View 模型中,例如您的全名属性,这在 View 模型中是正确的。但是,如果您有任何需要域逻辑的函数/计算,那么我认为 View 模型不适合它,它可以驻留在域模型中以实现可重用性,也可以驻留在业务逻辑层中的其他地方。
所以简短的回答 - View 模型中的 View 格式,其他地方的任何域逻辑。
