C# 如何向视图传递多个参数?
我在ActionResult中传递两个列表变量,如下所示C# 如何向视图传递多个参数?,c#,.net,asp.net-mvc,C#,.net,Asp.net Mvc,我在ActionResult中传递两个列表变量,如下所示 public ActionResult Index() { List<category> cat = _business.ViewAllcat().ToList(); List<Books> book = _business.ViewAllBooks().ToList(); return View(book); } public ActionResult Index() { List ca
public ActionResult Index()
{
List<category> cat = _business.ViewAllcat().ToList();
List<Books> book = _business.ViewAllBooks().ToList();
return View(book);
}
public ActionResult Index()
{
List cat=_business.ViewAllcat().ToList();
列表簿=_business.ViewAllBooks().ToList();
返回视图(书);
}
当我运行代码时,我得到以下错误
传递到字典中的模型项的类型为
System.Collections.Generic.List1[Durgesh_Bhai.Models.category],但是
此词典需要类型为的模型项
System.Collections.Generic.IEnumerable1[Durgesh_Bhai.Models.Books]
当我在Actionresult中只使用一个列表时,它工作得很好。我也是mvc新手,但我得到的解决方案是创建一个类,该类将所有必需的对象作为数据成员保存,并传递该类的对象 我创建了一个名为data的类,并将所有对象分配给该类的对象,然后将该对象发送到模型 或者你可以使用查看包
Class Data
{
public List<category> cat {get;set;}
public List<Books> book {get;set;}
public Data()
{
this.cat = new List<category>();
this.book = new List<Books>();
}
}
public ActionResult Index()
{
Data d=new Data();
d.cat = _business.ViewAllcat().ToList();
d.book = _business.ViewAllBooks().ToList();
return View(d);
}
类数据
{
公共列表cat{get;set;}
公共列表簿{get;set;}
公共数据()
{
this.cat=新列表();
this.book=新列表();
}
}
公共行动结果索引()
{
数据d=新数据();
d、 cat=_business.ViewAllcat().ToList();
d、 book=_business.ViewAllBooks().ToList();
返回视图(d);
}
创建一个包含两个列表的新对象
public ActionResult Index()
{
List<category> cat = _business.ViewAllcat().ToList();
List<Books> book = _business.ViewAllBooks().ToList();
return View(new CatBooks { Cats = cat, Books = book });
}
public class CatBooks {
public List<category> Cats { get; set; }
public List<Books> Books { get; set; }
}
public ActionResult Index()
{
List cat=_business.ViewAllcat().ToList();
列表簿=_business.ViewAllBooks().ToList();
返回视图(新CatBooks{Cats=cat,Books=book});
}
公共类教材{
公共列表猫{get;set;}
公共列表书籍{get;set;}
}
您应该创建一个ViewModel类(只是一个.cs类),其中包含页面上需要的所有内容
然后在视图的第一行,您应该使用viewmodel类作为您的模型
然后在控制器操作中填充模型,如:
public ActionResult Index()
{
List<category> cat = _business.ViewAllcat().ToList();
List<Books> book = _business.ViewAllBooks().ToList();
return View(new MyViewModel() { Cats = cat, Books = book);
}
public ActionResult Index()
{
List cat=_business.ViewAllcat().ToList();
列表簿=_business.ViewAllBooks().ToList();
返回视图(新的MyViewModel(){Cats=cat,Books=book);
}
然后您就可以访问页面上的所有内容。请创建一个新的ViewModel类,并按如下方式存储您的两个列表:
public class MyViewModel
{
public List<Category> Categories { get; set; }
public List<Book> Books { get; set; }
public MyViewModel()
{
this.Categories = new List<Category>();
this.Books = new List<Book>();
}
}
public ActionResult Index()
{
MyViewModel model = new MyViewModel();
model.Categories = _business.ViewAllcat().ToList();
model.Books = _business.ViewAllBooks().ToList();
return View(model);
}
公共类MyViewModel
{
公共列表类别{get;set;}
公共列表书籍{get;set;}
公共MyViewModel()
{
this.Categories=新列表();
this.Books=新列表();
}
}
公共行动结果索引()
{
MyViewModel模型=新的MyViewModel();
model.Categories=_business.ViewAllcat().ToList();
model.Books=_business.ViewAllBooks().ToList();
返回视图(模型);
}
然后在视图(index.cshtml)中声明MyViewModel,如下所示:
@model WebApp.Models.MyViewModel
<div>
your html
</div>
@model WebApp.Models.MyViewModel
你的html
我们刚才使用的概念称为视图模型。请在此处阅读更多信息:
看看这个,我想问题在于视图。你能在这里发布你的索引视图吗