简体   繁体   English

为部分视图MVC填充数据

[英]Filling Data For Partial View MVC

We're developing news website we're confused with some concept of usage. 我们正在开发新闻网站,我们对某些用法的概念感到困惑。 I'd like to ask and know better if possible. 我想问一下,如果可能的话更好地了解。 We've a homepage which may contain a lot of models at once so we're separating our homepage to partial views and we're planning to feed them with the appropriate models. 我们有一个主页,其中可能一次包含许多模型,因此我们将主页分为部分视图,并计划为它们提供适当的模型。

In one partial we're enumerating in categories that are not marked as deleted and we've two types of categories. 在一部分中,我们枚举未标记为已删除的类别,并且我们有两种类别。 One of them displays the latest post and the other displays 4 posts at once. 其中一个显示最新的帖子,另一个同时显示4个帖子。 We've achieved this actually but as i've mentioned we would like to know if there is a better way or if we're doing anything wrong because right now we're keeping the connection to the context open until the partial is rendered. 我们实际上已经实现了这一点,但是正如我已经提到的,我们想知道是否有更好的方法,或者我们做错了什么,因为现在我们一直保持与上下文的连接打开,直到呈现部分。

Here is the code for views 这是视图的代码

Partial View Code (CategoryRepeater.cshtml) 部分查看代码(CategoryRepeater.cshtml)

@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();
}
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.

Separate your concerns. 分开关注。 You can see this project for start: http://www.codeproject.com/Tips/617361/Partial-View-in-ASP-NET-MVC 您可以看到该项目的开始: http : //www.codeproject.com/Tips/617361/Partial-View-in-ASP-NET-MVC

Controller 控制者

      @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);


        }

Model 模型

public class CategoriesModel
                {
                    public List<PostCategories> NotDeletedCategories {get;set;}
                    public List<PostCategories> DeletedCategories {get;set;}
                };

View 视图

@model CategoriesModel
@Html.RenderPartial("DeletedCategories", Model.DeletedCategories)
@Html.RenderPartial("NotDeletedCategories", Model.NotDeletedCategories)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM