我们正在开发新闻网站,我们对一些使用概念感到困惑。如果可能的话,我想问一下,知道的更好。我们有一个可能同时包含很多模型的主页,因此我们将主页与部分视图分开,我们计划用适当的模型为它们提供信息。
在一个部分中,我们列举了未标记为已删除的类别,我们有两种类别。其中一个显示最新的帖子,另一个显示4个帖子。我们实际上已经实现了这一点,但正如我所提到的,我们想知道是否有更好的方法或者我们做错了什么,因为现在我们保持与上下文的连接打开,直到部分呈现为止。
以下是视图的代码
@using SosyalGundem.WebUI.DatabaseContext;
@{
var categoryList = new List<PostCategories>();
var db = new SosyalGundemDb();
categoryList = db.PostCategories.Include("Posts").Where(x => !x.IsDeleted).ToList();
}
@for (int i = 0; i < categoryList.Count; i++)
{
if (i % 3 == 0 || i == 0)
{
@Html.Raw("<div class=\"row-fluid spacer\">")
}
var category = categoryList[i];
if (category.PostCategoryType == 1)
{
<div class="span4">
<h3 class="title"><span>@category.PostCategoryName</span></h3>
@{
var article = category.Posts.FirstOrDefault();
if (article != null)
{
<article class="post">
<div class="entry clearfix">
<div class="span6">
<a href="#" title="Permalink to Suspen disse auctor dapibus neque pulvinar urna leo" rel="bookmark">
<img width="225" height="136" src="@Url.Content("~/Content/uploadedimages/" + article.Media.ToList()[0].MediaContent )" alt="shutterstock_70184773" />
</a>
</div>
<div class="span6">
<h4 class="smallnewstitle">@article.PostTitle</h4>
<p>@(article.PostSummary.Length > 100 ? article.PostSummary.Substring(0, 100) : article.PostSummary)</p>
<div class="meta">
<span class="date">@article.PostDate.ToString("MMMM dd, yyyy")</span>
</div>
</div>
</div>
</article>
}
}
</div>
}
else
{
<div class="video-box widget span4">
<h3 class="title"><span>@category.PostCategoryName</span></h3>
@{
int cati = 0;
var firstPost = category.Posts.OrderByDescending(x => x.PostDate).FirstOrDefault();
}
@if (firstPost != null)
{
<h4 class="smallnewstitle">@firstPost.PostTitle</h4>
<p>@(firstPost.PostSummary.Length > 100 ? firstPost.PostSummary.Substring(0, 100) : firstPost.PostSummary) </p>
<ul>
@foreach (var item in category.Posts.OrderByDescending(x => x.PostDate))
{
if (cati <= 3)
{
<li>
<a href="#" title="@item.PostTitle" rel="bookmark">
<img width="225" height="136" src="@Url.Content("~/Content/images/dummy/shutterstock_134257640-225x136.jpg")" alt="shutterstock_134257640" />
</a>
</li>
}
else
{
break;
}
cati++;
}
</ul>
}
</div>
}
if (i % 3 == 0 && i != 0)
{
@Html.Raw("</div>")
}
}
@{
db.Dispose();
}
答案 0 :(得分:0)
Hi Jinava,
I would suggest bind Model to the View,
Like,
public ActionResult CategoryRepeater()
{
var multiViewModel = new MultiViewModelModel
{
ModelForParialView1= new XYZ(),
ModelForParialView2= new PQR()
};
return View(model);
}
For the View
@model MultiViewModelModel
And then PAss the views with the MultiViewModelModel.ModelForParialView1 and MultiViewModelModel.ModelForParialView2
You can perform all the model operations on the view.
And at the controller level perform all the database operations and release the database connection there itself no need to get that on the view.
Hope this explanation helps you.
答案 1 :(得分:0)
分开您的疑虑。您可以看到此项目的开始:http://www.codeproject.com/Tips/617361/Partial-View-in-ASP-NET-MVC
控制器
@using SosyalGundem.WebUI.DatabaseContext;
public ActionResult SomeAction()
{
var model = new CategoriesModel
{
NotDeletedCategories = db.PostCategories.Include("Posts").Where(x => !x.IsDeleted).ToList(),
DeletedCategories = db.PostCategories.Include("Posts").Where(x => x.IsDeleted).ToList()
};
return View(model);
}
模型
public class CategoriesModel
{
public List<PostCategories> NotDeletedCategories {get;set;}
public List<PostCategories> DeletedCategories {get;set;}
};
查看
@model CategoriesModel
@Html.RenderPartial("DeletedCategories", Model.DeletedCategories)
@Html.RenderPartial("NotDeletedCategories", Model.NotDeletedCategories)