ASP.NET MVC,创建将多个对象返回到视图的View方法

[英]ASP.NET MVC, create View method that would return multiple objects to the view


Is there a way to create a View() method that would return multiple objects, for example, I would like to call it something like this:

有没有办法创建一个返回多个对象的View()方法,例如,我想称之为:

public class HomeController : Controller
{
  public ActionResult Index()
  {
    return View(CustomObject1 customObject1, CustomObject2 customObject2);
  }
}

2 个解决方案

#1


6  

Yes, it's possible, just create a view model:

是的,这是可能的,只需创建一个视图模型:

public class MyViewModel
{
    public CustomObject1 CustomObject1 { get; set; }
    public CustomObject2 CustomObject2 { get; set; }
}

which you will pass to the view:

您将传递给视图:

public ActionResult Index()
{
    var model = new MyViewModel();
    model.CustomObject1 = customObject1;
    model.CustomObject2 = customObject2;
    return View(model);
}

and finally make your view strongly typed to this view model:

最后使您的视图强烈输入此视图模型:

@model MyViewModel

and access the corresponding properties when needed:

并在需要时访问相应的属性:

 <div>@Model.CustomObject1.FoorBar</div>

#2


0  

you can do Like @Darin dimitrov mentioned or you can use tuple for passing more than one model to your view page, Tuple is generic collection

你可以像@Darin dimitrov那样提到或者你可以使用元组将多个模型传递到你的视图页面,Tuple是泛型集合

how to use tuple, you can follow this link

如何使用元组,你可以按照这个链接

Thanks, hope it will help for you

谢谢,希望它对你有所帮助

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2015/12/29/4f22586d94f25dac5b6df3887fa1ecc2.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告