假设我有一个像这样的界面:
interface IThing {
int Id { get; set; }
string Title { get; set; }
}
在 ASP.NET MVC 中,我有一个表单可以像这样发布到 Controller 操作:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult NewThing([Bind(Exclude = "Id")] SimpleThing thing) {
// code to validate and persist the thing can go here
}
SimpleThing 是一个具体的类,它几乎没有实现 IThing。
但是,我希望我所有的方法都能处理接口(interface)。我有一个使用 NHiberate 及其自己的 IThing 实现(我们称它为 RealThing)的数据集。我无法将 SimpleThing 传递给它,因为它会提示“未知实体”。
有没有人对更简洁的方法有任何想法?我在考虑使用工厂类的一些事情。但是我如何让 MVC 表单绑定(bind)器使用它呢?
谢谢!
最佳答案
您可以使用自定义 model binders .但是msdn上的文章完全没用。所以最好使用搜索并找到更好的东西。有大量文章可用。
关于asp.net-mvc - ASP.NET MVC : avoid tight coupling when binding form POST to parameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1080925/