[英]ASP.NET MVC. Multiple results from the same repository in one View
I'm absolutely beginner in ASP.NET MVC. 我绝对是ASP.NET MVC的初学者。 Here what I'm trying to do for the main (home) page: 这是我要为主页(主页)做的事情:
As I know it's impossible to use different models in same View, so I'm trying to make calls for different methods of the same repository and then show results. 据我所知,不可能在同一View中使用不同的模型,因此我试图对同一存储库的不同方法进行调用,然后显示结果。 But the problems is - I don't know how to join results. 但是问题是-我不知道如何加入结果。 Should I create new model and pass to View() or should I use partial view with different model? 我应该创建新模型并传递给View()还是应该将局部视图与其他模型一起使用? Here is the Controller code: 这是控制器代码:
public ActionResult Index()
{
var top20 = PostRepository.GetLast20();
var tag = PostRepository.GetTags();
//How should I return to View() both top20 and tags?
return View();
}
PS ActionResult Index contain wrong parameter, so I delete it. PS ActionResult Index包含错误的参数,因此我将其删除。 PSS Probably I should show my repository: PSS可能我应该显示我的存储库:
public class BlogPostRepository : IPostRepo, IDisposable
{
private BlogAspNet db;
public BlogPostRepository(BlogAspNet db)
{
this.db = db;
}
public IQueryable<blog_post> GetAll()
{
return db.blog_post.AsQueryable();
}
public IQueryable<tags> GetTags()
{
return db.tags.AsQueryable();
}
public IQueryable<ShortPostInfo> GetLast20()
{
var last = from a in db.blog_post
orderby a.Posted descending
select new ShortPostInfo
{
PostID = a.ID,
PostSubject = a.Subject,
PostAuthor = (from x in db.users where x.ID == a.Author select x.Login).FirstOrDefault(),
PostCreated = a.Posted,
PostImage = a.PostAvatar !=null ? a.PostAvatar : "other image",
PostRating = a.Rating != null ? a.Rating : 0,
PostedTags = (from x in db.posted_tags
join y in db.tags on x.TagID equals y.ID
where x.PostID == a.ID
select y.TagName).ToList()
};
return last.Take(20).AsQueryable();
}
And here is interface: 这是接口:
public class ShortPostInfo
{
public int PostID { get; set; }
public string PostSubject { get; set; }
public DateTime? PostCreated { get; set; }
public string PostImage { get; set; }
public string PostAuthor { get; set; }
public byte? PostRating { get; set; }
public IList<string> PostedTags { get; set; }
}
public interface IPostRepo : IDisposable
{
IQueryable<blog_post> GetAll();
IQueryable<tags> GetTags();
IQueryable<ShortPostInfo> GetLast20();
IQueryable<ShortPostInfo> GetPostByTag(int tagid);
IQueryable<FullPostInfo> GetPostById(int id);
void Add(blog_post item);
void Update(blog_post item);
void Remove(int id);
}
You can return both results by using an anonymous objects as the model: 您可以通过使用匿名对象作为模型来返回两个结果:
return View(new { top20, tag });
Or if you prefer something stronly typed, you can declare a class to encapsulate your properties: 或者,如果您更喜欢stronly类型的内容,则可以声明一个类来封装您的属性:
public IndexModel {
public IEnumerable<Post> Top20 { get;set;}
public IEnumerable<Tag> Tags { get;set;}
}
Then in your Index action: 然后在您的索引操作中:
public ActionResult Index(ShortPostInfo m)
{
var top20 = PostRepository.GetLast20();
var tag = PostRepository.GetTags();
return View(new IndexModel { Top20 = top20, Tags = tag });
}
Finally, you can access your model in your Razor view by using this notation at the top of the file: 最后,您可以使用文件顶部的以下标记在Razor视图中访问模型:
@model IndexModel
And accessing properties by using Model object: 并通过使用Model对象访问属性:
<ul>
@foreach(var post in Model.Top20) {
<li>@post.Title</li>
}
</ul>
See this article to get further explanation about it (especially "Strongly Typed Models and the @model Keyword" chapter) . 请参阅本文以获取有关它的进一步说明(尤其是“严格类型化的模型和@model关键字”一章)。
You could create a view model where you populate with the data strictly necessary to be displayed in your view. 您可以创建一个视图模型,在其中填充严格必要的数据以在视图中显示。 You would map the fields from each DTO into your view model and render those in your view. 您将把每个DTO中的字段映射到视图模型中,并在视图中呈现这些字段。 You could also as next step use AutoMapper to automatically map your properties. 您还可以在下一步中使用AutoMapper自动映射属性。
https://nerddinnerbook.s3.amazonaws.com/Part6.htm https://nerddinnerbook.s3.amazonaws.com/Part6.htm
http://jasona.wordpress.com/2010/02/05/getting-started-with-automapper/ http://jasona.wordpress.com/2010/02/05/getting-started-with-automapper/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.