当声明类型不同时,UpdateModel不起作用

时间:2013-07-13 04:28:30

标签: c# .net asp.net-mvc asp.net-mvc-4

这是我要问的代码示例。

    public ActionResult Action()
    {
        object person = new Person(); //It works if i replace object with Person
        UpdateModel(person); //this does not update person because of "object" declaring type

        return View();
    }

如果我在运行时确定模型类型,更新模型的最佳方法是什么?

1 个答案:

答案 0 :(得分:1)

要在运行时解决(虽然不明显为什么你需要,从你发布的内容),然后使用dynamic

dynamic person = new Person();   // resolves at runtime -- no point in doing this,
                                 // since the type is known at compile time anyway