Вот пример, с которого можно начать:
Модель:
public class PopularTutorial
{
public int ID { get; set; }
public int NumberOfReads { get; set; }
public string Title { get; set; }
}
public class Blog
{
public int ID { get; set; }
public string Head { get; set; }
public string PostBy { get; set; }
public string Content { get; set; }
}
Контроллер:
public class ArticlesController : Controller
{
public ActionResult Index()
{
return View();
}
[ChildActionOnly]
public ActionResult Blogs()
{
return PartialView(GetAllBlogEntries());
}
[ChildActionOnly]
public ActionResult Popular()
{
return PartialView(GetPopularBlogs());
}
private IEnumerable<PopularTutorial> GetPopularBlogs()
{
return new[]
{
new PopularTutorial { ID = 17, Title = "Test1", NumberOfReads = 1050 },
new PopularTutorial { ID = 18, Title = "Test2", NumberOfReads = 5550 },
new PopularTutorial { ID = 19, Title = "Test3", NumberOfReads = 3338 },
new PopularTutorial { ID = 20, Title = "Test4", NumberOfReads = 3338 },
new PopularTutorial { ID = 21, Title = "Test5", NumberOfReads = 3338 },
new PopularTutorial { ID = 22, Title = "Test6", NumberOfReads = 3338 },
new PopularTutorial { ID = 23, Title = "Test7", NumberOfReads = 3338 },
};
}
private IEnumerable<Blog> GetAllBlogEntries()
{
return new[]
{
new Blog { ID = 1, Head = "Introduction to MVC", PostBy = "Lijo", Content = "This is a ..." },
new Blog { ID = 2, Head = "jQuery Hidden Gems", PostBy = "Lijo", Content = "This is a ..." },
new Blog { ID = 3, Head = "Webforms Intenals", PostBy = "Lijo", Content = "This is a ..." }
};
}
}
Просмотр (~/Views/Articles/Index.cshtml
):
All Blogs List
@Html.Action("blogs")
Popular Tutorial
@Html.Action("popular")
Частичные блоги (~/Views/Articles/Blogs.cshtml
):
@model IEnumerable<Blog>
<section>
<ul>
@Html.DisplayForModel()
</ul>
</section>
Шаблон отображения блога (~/Views/Articles/DisplayTemplates/Blog.cshtml
):
@model Blog
<li>
<h3>@Html.DisplayFor(x => x.Head)</h3>
@Html.DisplayFor(x => x.Content)
</li>
Популярное Частичное (~/Views/Articles/Popular.cshtml
):
@model IEnumerable<PopularTutorial>
@{
var grid = new WebGrid(Model, canPage: true, canSort: false, rowsPerPage: 3);
}
@grid.GetHtml(
columns: grid.Columns(
grid.Column("", format: @<text>@item.Title</text>)
)
)
Результат:
![enter image description here](https://i.stack.imgur.com/gYQ4D.png)
UPDATE:
В соответствии с просьбой в разделе комментариев я постараюсь охватить 2 дополнительных сценария:
1) Создать отдельный контроллер для Popular?
Это довольно просто. Просто создайте новый PopularBlogs
контроллер:
public class PopularBlogsController : Controller
{
public ActionResult Popular()
{
return PartialView(GetPopularBlogs());
}
private IEnumerable<PopularTutorial> GetPopularBlogs()
{
return new[]
{
new PopularTutorial { ID = 17, Title = "Test1", NumberOfReads = 1050 },
new PopularTutorial { ID = 18, Title = "Test2", NumberOfReads = 5550 },
new PopularTutorial { ID = 19, Title = "Test3", NumberOfReads = 3338 },
new PopularTutorial { ID = 20, Title = "Test4", NumberOfReads = 3338 },
new PopularTutorial { ID = 21, Title = "Test5", NumberOfReads = 3338 },
new PopularTutorial { ID = 22, Title = "Test6", NumberOfReads = 3338 },
new PopularTutorial { ID = 23, Title = "Test7", NumberOfReads = 3338 },
};
}
}
, а затем переместите частичку ~/Views/Articles/Popular.cshtml
, показанную ранее, в ~/Views/PopularBlogs/Popular.cshtml
и, наконец, обновите местоположение в ~/Views/Articles/Index.cshtml
:
All Blogs List
@Html.Action("blogs")
Popular Tutorial
@Html.Action("popular", "popularblogs")
2) Сделайте звонок популярному как ajax
В вашем представлении ~/Views/Articles/Index.cshtml
замените помощника Html.Action
, который отображает популярные блоги, на div
:
All Blogs List
@Html.Action("blogs")
Popular Tutorial
<div id="popular" data-url="@Url.Action("Popular", "PopularBlogs")"></div>
и затем измените ~/Views/PopularBlogs/Popular.cshtml
, чтобы включить нумерацию страниц AJAX:
@model IEnumerable<PopularTutorial>
@{
var grid = new WebGrid(
Model,
canPage: true,
canSort: false,
rowsPerPage: 3,
ajaxUpdateContainerId: "grid"
);
}
@grid.GetHtml(
htmlAttributes: new { id = "grid" },
columns: grid.Columns(
grid.Column("", format: @<text>@item.Title</text>)
)
)
И последний шаг - загрузить содержимое этой части в соответствующий div:
$(function () {
var popular = $('#popular');
popular.load(popular.data('url'));
});