2013-07-13 25 views
0

這是我所問的代碼示例。當聲明類型不同時,UpdateModel不起作用

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(); 
    } 

如果我在運行時確定模型類型,更新模型的最佳方法是什麼?

+0

看到它 http://stackoverflow.com/questions/7436822/update-model-from-database-does-see-a-type-change – 2013-07-13 04:30:37

回答

1

要解決在運行時(雖然不是很明顯,爲什麼你需要,從您發佈的),然後使用dynamic

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

var關鍵字與運行時無關。動態關鍵字工作相當好。所以你可以請更新你的答案,以獲得明確的答案。 – Freshblood

+0

@新鮮血液,你是老闆......更新。 – McGarnagle

+0

:)它是動態關鍵字處理問題的最佳方式之一 – Freshblood