如何将通用参数传递到局部视图 [英] How to pass generic parameter to partial view
本文介绍了如何将通用参数传递到局部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
泛型部分视图:如何设置泛型类作为模型?
Possible Duplicate:
Generic partial view: how to set a generic class as model?
我正在尝试使用泛型类型构建通用功能,但遇到了以下情况.
I am trying to build common functionality using generic types but got stuck with below scenario.
查看模型
public class DeleteForm<T>
{
public LogInfo Ticket { get; set; }
public string Id { get; set; }
public DeleteForm() {
Ticket = new LogInfo();
}
public DeleteForm(T viewModel) : this() {
ViewModel = viewModel;
}
public T ViewModel { get; set; }
}
控制器
public ActionResult Index(string name)
{
return View("index", new DeleteForm<List<Users>>(new List<Users>());
}
列表屏幕
@model DeleteForm<List<Users>>
//gridview displays list of users
@Html.Partial("revisionwindow", Model)
局部视图
@model DeleteForm<T> <---Its not working
@Html.EditorFor(o=>o.Ticket)
@Html.EditorFor(o=>o.Id)
推荐答案
如果要传递模型进行查看,则必须使用强类型(特定类型)的模型.
因此SomeClass<T>
类型将不起作用.可以使用基类代替泛型类型来填充
您的要求.我的意思是:
查看模型
If you pass a model to view, it has to be strongly-typed (particular type).
So SomeClass<T>
type won't work. Instead of generic type a base class could fill
your requirements. What I mean is:
View Model
public abstract class Form
{
public Form()
{
Ticket = new LogInfo();
}
public LogInfo Ticket {get; set;}
public int Id {get; set;}
}
public class DeleteUsersForm: Form
{
public DeleteUsersForm(IEnumerable<Users> users):base()
{
this.ViewModel = users;
}
public IEnumerable<Users> ViewModel {get; set;}
}
控制器
public ActionResult Index(string name)
{
return View(new DeleteUsersForm(new List<Users>()));
}
列表屏幕
@model DeleteUsersForm
//displays list
@Html.Partial("revisionwindow", Model)
局部视图
@model Form
@Html.EditorFor(o=>o.Ticket)
@Html.EditorFor(o=>o.Id)
这篇关于如何将通用参数传递到局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文