C# 在视图定义中使用类型参数
我在一个数据库中有几个维护表,希望为所有这些表设置可重用的视图。我想做一些类似于下面的代码片段的事情,但不知道如何从Razor/HTML传递类型C# 在视图定义中使用类型参数,c#,asp.net,asp.net-mvc,entity-framework,razor,C#,Asp.net,Asp.net Mvc,Entity Framework,Razor,我在一个数据库中有几个维护表,希望为所有这些表设置可重用的视图。我想做一些类似于下面的代码片段的事情,但不知道如何从Razor/HTML传递类型 public async Task<ActionResult> Index<T>() where T : class { return View(await db.Set<T>().ToListAsync()); } 传入字典的模型项的类型为“System.Collections.Generic.List
public async Task<ActionResult> Index<T>() where T : class
{
return View(await db.Set<T>().ToListAsync());
}
传入字典的模型项的类型为“System.Collections.Generic.List1[System.Object]”,但此字典需要类型为“System.Collections.Generic.IEnumerable
1[MyType]”的模型项
第二,我试着通过打字
@Html.ActionLink("Title", "Action", "Controller", new { type = typeof(MyType) }, null)
public async Task<ActionResult> Index(Type type)
{
return View(await db.Set(type).ToListAsync());
}
@Html.ActionLink(“Title”、“Action”、“Controller”,新的{type=typeof(MyType)},null)
公共异步任务索引(类型)
{
返回视图(wait db.Set(type).toListSync());
}
这根本不起作用。参数type
为空
关于一种新的方法或者如何使上述方法发挥作用,您有什么想法吗?
等待db.Set(Type.GetType(Type)).ToListAsync()
Set async方法返回什么?还可以显示ViewModel类吗?这一行抛出了我在上面引用的错误“模型项传递到字典…”。在每个场景中,ViewModel中的相关代码都粘贴在HTML的正下方-ViewModel只有一个索引
方法
@Html.ActionLink("Title", "Action", "Controller", new { type = typeof(MyType) }, null)
public async Task<ActionResult> Index(Type type)
{
return View(await db.Set(type).ToListAsync());
}