【发布时间】:2011-04-30 01:25:15
【问题描述】:
如果我有以下强类型视图:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<XXX.DomainModel.Core.Locations.Location>" %>
Location 是一个抽象类。
我有以下控制器,它通过 POST 接受强类型模型:
[HttpPost]
public ActionResult Index(Location model)
我收到一个运行时错误,指出 “无法创建抽象类
这当然是有道理的。但是 - 我不确定这里最好的解决方案是什么。
我有很多具体类型(大约 8 个),这是一个只能编辑抽象类属性的视图。
我尝试做的是为所有不同的具体类型创建重载,并以通用方法执行我的逻辑。
[HttpPost]
public ActionResult Index(City model)
{
UpdateLocationModel(model);
return View(model);
}
[HttpPost]
public ActionResult Index(State model)
{
UpdateLocationModel(model);
return View(model);
}
等等等等
然后:
[NonAction]
private void UpdateLocationModel (Location model)
{
// ..snip - update model
}
但这也不起作用,MVC 抱怨操作方法不明确(也有道理)。
我们做什么?我们可以不绑定到抽象模型吗?
【问题讨论】:
-
好问题。有兴趣看看答案!
-
我很好奇您是否找到了更好的方法来处理这个问题?
-
@Mystere Man - 不。我不必再这样做了。如果我这样做了,我会按照公认答案的建议去做。
标签: c# .net asp.net-mvc-2 abstract-class model-binding