如何将多个参数传递给MVC 4中的视图? [英] How to pass multiple parameters to a view in MVC 4?
问题描述
嗨
我在ActionResult中传递两个列表变量如下所示。
Hi
I am passing two list variables in an ActionResult as Below.
public ActionResult Index()
{
List<category> cat = _business.ViewAllcat().ToList();
List<Books> book = _business.ViewAllBooks().ToList();
return View(book);
}
当我运行代码时,我收到以下错误
传入字典的模型项的类型为'System.Collections.Generic.List`1 [Durgesh_Bhai.Models.category]',但此字典需要一个类型为'System.Collections.Generic.IEnumerable的模型项`1 [Durgesh_Bhai.Models.Books]'。
但是当我在Actionresult中只使用一个List时它的工作正常。
请提供相同的解决方案,因为这是一项紧急要求。
When I run the Code I get the below error
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[Durgesh_Bhai.Models.category]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Durgesh_Bhai.Models.Books]'.
But when I am using only one List in Actionresult its working fine.
Please provide the solution for the same as this is a urgent requirement.
推荐答案
你好,
我建议使用TupleList返回多个List并访问它。
只需执行以下操作即可... 。
Hello,
I Suggest to use TupleList for Return more than one List and access it.
Just do Following...
public ActionResult Index()
{
List<category> cat = _business.ViewAllcat().ToList();
List<Books> book = _business.ViewAllBooks().ToList();
var objTuple=new Tuple<List<category>,List<Books>>(cat,book);
return View(objTuple);
}
// Access TupleList at Index.cshtml
@model Tuple<List<cat>,List<book>>
在这种情况下,最好使用ViewModel,创建一个包含require实体的viewmodel在View中并在控制器上设置值并将ViewModel传递给View。
Better you use ViewModel in this case , Create a viewmodel with require entities in your View and set values on controller and pass ViewModel to View.
这篇关于如何将多个参数传递给MVC 4中的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!