我在模型中有一个List<string>
当我写一个html helper时,我可以从metadata.Model获得数据,List<string>是一个对象

// this is from MVC3 (namespace System.Web.Mvc -> ModelMetadata), I did not write this
// Summary:
//     Gets the value of the model.
//
// Returns:
//     The value of the model. For more information about System.Web.Mvc.ModelMetadata,
//     see the entry ASP.NET MVC 2 Templates, Part 2: ModelMetadata on Brad Wilson's
//     blog
public object Model { get; set; }

我的问题是:如何从Object获取ojit_code?

最佳答案

如果object变量的基础类型是List<string>,则将执行简单的强制转换:

// throws exception if Model is not of type List<string>
List<string> myModel = (List<string>)Model;

或者
// return null if Model is not of type List<string>
List<string> myModel = Model as List<string>;

10-07 16:31
查看更多