首先,我有一个模型,它是我专门为视图创建的视图模型,这不是真正的模型映射到表格如下:
型号:
public class BlogPageModel
{
public Blog Blogs { get; set; }
public BlogAid BlogAids { get; set; }
public BlogAuthor BlogAuthors { get; set; }
public BlogComment BlogComments { get; set; }
public BlogContent BlogContents { get; set; }
public BlogNews BlogNews { get; set; }
public BlogTitle BlogTitles { get; set; }
}
但你可以看到这个模型中的所有属性都是真实的模型映射到表格,所以我用它来显示我的视图内容。
另一方面,我有一个视图,这是一个页面,显示博客作者的所有博客,谁应该是博客所有者。所以它就像下面这样:
查看:
@model IEnumerable<LeduInfo.Models.BlogPageModel>
@{
ViewBag.Title = "Index";
}
<div class="container">
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Blog Home 1 <small>Blog Homepage</small></h1>
<ol class="breadcrumb">
<li><a href="index.html">Home</a></li>
<li class="active">Blog Home 1</li>
</ol>
</div>
</div>
<div class="row">
<div class="col-lg-8">
@foreach (var item in Model)
{
<h1>@*<a href="blog-post.html">A Blog Home Template for Bootstrap 3</a>*@
@Html.ActionLink(item.BlogTitles.Titles,"BlogPost",new{@id=item.Blogs.BlogID})
</h1>
<p class="lead">by <a href="#">Start Bootstrap</a></p>
<hr>
<p><i class="icon-time"></i> Posted on August 28, 2013 at 10:00 PM</p>
<hr>
<a href="blog-post.html"><img src="http://placehold.it/900x300" class="img-responsive"></a>
<hr>
<p>This is a very basic starter template for a blog homepage. It makes use of Font Awesome icons that are built into the 'Modern Business' template, and it makes use of the Bootstrap 3 pager at the bottom of the page.</p>
<a class="btn btn-primary" href="blog-post.html">Read More <i class="icon-angle-right"></i></a>
<hr>
}
我的问题是如何使模型枚举并返回model.tolist()然后发送回视图进行显示?
控制器:
public ActionResult Index()
{
List<BlogPageModel> modelList = new List<BlogPageModel>();
BlogPageModel blogpagemodel;
var blogPageModelList = from b in DB.Blogtbl
join t in DB.BlogTitletbl on b.TitleID equals t.TitleID
join c in DB.BlogContenttbl on b.ContentID equals c.ContentID
where b.BlogID > 0
select new { Title = t.Titles, Content = c.Contents, id = b.BlogID };
?????
return View();
}
你有什么好主意吗?
答案 0 :(得分:0)
很抱歉我不知道你的查询返回值是多少。但我会发布示例代码
public ActionResult Index()
{
List<BlogPageModel> modelList = new List<BlogPageModel>();
modelList.Blogs.Add(//Blogs table values);
modelList.BlogAid.Add(//BlogAid table values);
modelList.BlogAuthor.Add(//BlogAuthor table values);
List<BlogPageModel> modelListLatest = new List<BlogPageModel>();
return View(modelListLatest.AddRange(modelList));
}
答案 1 :(得分:0)
问题是您的Linq查询returns an anonymous type,因此您无法将该类型的名称写入View中,以便以强类型方式访问它。如果您选择BlogPageModel
,则可以返回如下的可枚举:
public ActionResult Index()
{
var blogPageModelList = (
from b in DB.Blogtbl
join t in DB.BlogTitletbl on b.TitleID equals t.TitleID
join c in DB.BlogContenttbl on b.ContentID equals c.ContentID
where b.BlogID > 0
select new BlogPageModel
{
BlogTitles = t.Titles,
BlogContents = c.Contents
})
.ToList();
return View(blogPageModelList);
}
这应该适用于你的观点,因为它写在你的问题中。
答案 2 :(得分:0)
通过我有一个答案可以打开我的想法,但我想发布正确而详细的解释我如何解决我的问题。
IQueryable<BlogPageModel> blogmodellist = from b in DB.Blogtbl
join t in DB.BlogTitletbl on b.TitleID equals t.TitleID
join c in DB.BlogContenttbl on b.ContentID equals c.ContentID
//join m in DB.BlogCommenttbl on b.CommentID equals m.CommentID
where b.BlogID > 0 && t.Titles !=null
select new BlogPageModel { BlogTitles = t , BlogContents = c, Blogs=b};
blogpagemodel.Blogs = new Blog();
blogpagemodel.BlogTitles = new Models.BlogTitle();
blogpagemodel.BlogContents = new BlogContent();
//IEnumerable<BlogPageModel> titleList = blogPageModelList.TakeWhile(m => m.Title != null);
foreach (var item in blogmodellist)
{
//blogpagemodel.BlogComments.CommentID = (int)item.cid;
blogpagemodel.Blogs.BlogID = item.Blogs.BlogID;
blogpagemodel.BlogTitles.Titles = item.BlogTitles.Titles;
blogpagemodel.BlogContents.Contents = item.BlogContents.Contents;
modelList.Add(blogpagemodel);
}
//var titlesList = blogPageModelList
// .Select(m => m.Title)
// .ToList();
//modelList.AddRange(blogPageModelList.ToList());
return View(blogmodellist.ToList());