【发布时间】:2010-03-25 12:23:30
【问题描述】:
我想将我的具体课程与我的观点分开。不使用强类型视图,我很好。我只是在控制器方法签名中使用了一个大参数列表,然后使用我的服务层工厂方法来创建我的具体对象。
这实际上对我来说很好,但它让我思考,玩了一会儿之后,我意识到控制器方法实际上不可能接受接口作为方法参数 - 因为它无法实例化它。也无法通过 IDE 使用接口创建强类型视图(这实际上是有道理的)。
所以我的问题。有没有办法告诉控制器如何使用我的服务层工厂方法实例化接口参数?
我想转换自:
[Authorize]
[AcceptVerbs(HttpVerbs.Post)]
[UrlRoute(Path = "Application/Edit/{id}")]
public ActionResult Edit(String id, String TypeCode, String TimeCode, String[] SelectedSchoolSystems,
String PositionChoice1, String PositionChoice2, String PositionChoice3, String Reason, String LocationPreference,
String AvailableDate, String RecipientsNotSelected, String RecipientsSelected) {
//New blank app
IApplication _application = ApplicationService.GetById(id);
类似
[Authorize]
[AcceptVerbs(HttpVerbs.Post)]
[UrlRoute(Path = "Application/Edit/{id}")]
public ActionResult Edit(String id, IApplication app) {
//Don't need to do this anymore
//IApplication _application = ApplicationService.GetById(id);
【问题讨论】:
-
感谢大家的出色意见。巨大的帮助。
-
已经好几个月了,但经过更多的编码 - 我开始意识到即使自定义模型绑定器可能是正确的答案,使用强类型视图模型对我来说效果最好。关键是视图模型经常与我的数据库对象不同。
标签: asp.net-mvc