MVC3.0 Razor 单个View页返回多个model实体数据集

小记:单个View页面返回多个model数据集  做下笔记。

namespace Models

{

    public class Articel

    {

        public int Id { get; set; }

        [Required]

        [DisplayName("标题")]

        [MaxLength(100)]

        public string Title { get; set; }

    }



    public class Cate

    {

        public int CateId { get; set; }

        [DisplayName("文章类别")]

        [Required]

        public string CateName { get; set; }



        public List<Article> article { get; set; }

    }



    public class ListModel

    {

        /// <summary>

        /// 文章

        /// </summary>

        public IList<Article> artcleModel { get; set; }



        /// <summary>

        /// 文章类别

        /// </summary>

        public IList<Cate> cateModel { get; set; }



    }

}





Action方法编写如下:



 public ActionResult Index()

        {

            var model = new ListModel();

            model.artcleModel= Articel.ToList();

            model.cateModel= Cate.ToList();

            return View(model);

        }



View层引用如下:

@model Models.ListModel



@foreach (var tt in Models.artcleModel)

            {

             @tt.Title

            }



@foreach (var tt in Models.cateModel)

            {

              @tt.CateName

            }

 

你可能感兴趣的:(Model)