使用动态类型MVC强类型的视图 [英] using dynamic as type of MVC strongly typed view
问题描述
我有一个网页,创造动态的实体。
I have a page for creation of dynamic entities.
<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
...
我有两个动作:
public ActionResult Create()
{
dynamic model = ...
return View(model);
}
[HttpPost]
public ActionResult Create(dynamic(1) entity)
{
...
}
好了,问题是,实体来空从页面。如果我改变动态(1)真正的类型,它工作正常。
Well, the problem is that the entity comes empty from the page. If I change dynamic in (1) for the real type it works fine.
推荐答案
我不是100%,但我认为问题是,默认的模型绑定不知道该怎么做了动态类型,因为它不具有任何限定特性反映过来。你需要编写自己的模型绑定,将使用表单输入名称代替,这是很危险的/不可靠的,因为表格可在客户端进行修改。
I'm not 100% on this, but I think the problem is that the default model binder has no idea what to do with a 'dynamic' type, since it doesn't have any defined properties to reflect over. You'd need to write your own model binder that would use the form input names instead, which is dangerous/unreliable because the form can be modified on the client side.
我以前探索(这里所以实际上动态类型ViewPages:<一href=\"http://stackoverflow.com/questions/1178984/dynamic-typed-viewpage\">http://stackoverflow.com/questions/1178984/dynamic-typed-viewpage),和我来,它真的不给你在大多数情况下任何结论。至少,目前还没有(MVC 3+可能是不同的故事)。
I've explored dynamically-typed ViewPages before (here on SO actually: http://stackoverflow.com/questions/1178984/dynamic-typed-viewpage), and I've come to the conclusion that it really doesn't give you anything in most situations. At least, not yet (MVC 3+ could be different story).
下面是从菲尔哈克对此事的一些注意事项:的 http://haacked.com/archive/2009/08/26/method-missing-csharp-4.aspx
And here's some notes from Phil Haack on the matter: http://haacked.com/archive/2009/08/26/method-missing-csharp-4.aspx
这篇关于使用动态类型MVC强类型的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!