我想将模型传递给包含通用对象类型的布局和视图。我有以下内容;
public class BaseModel
{
public int ProductId {get;set;}
public Object ModelObject { get; set; }
}
public class ProductType1
{
public string Name {get;set;}
public decimal Price {get;set;}
}
public class ProductType2
{}
public ActionResult Index()
{
BaseModel baseModel = new BaseModel();
baseModel.ModelObject = new ProductType1();
return View("View1", "_MyLayOut", baseModel);
}
因此,在本例中,我将包含对象类型 ProductType1 的 baseModel 传递给布局。在我的布局顶部,我有
@model Project1.Models.BaseModel
在视图中,如何将 ModelObject 转换为 ProductType1,例如,我可以像 ProductType1.Name 一样引用模型。