结合ViewModels和部分视图

时间:2015-10-23 12:34:30

标签: c# asp.net-mvc-4

我有一个新闻页面,列出了最新新闻。在同一个视图中,我有一个具有表单的局部视图。我结合了两个ViewModel,试图让两个功能协同工作。但是,我收到了错误。

的ViewModels

public class Combined
{
    public Order SkipOrder { get; set; }
    public Credit Credit { get; set; }
    public List<NewsList> NewsList { get; set; }
}

public  class NewsList
{
    public int NewsId { get; set; }
    public byte NewsCategoryId { get; set; }
    public byte WebsiteId { get; set; }
    public string NewsTitle { get; set; }
    public string NewsStandFirst { get; set; }
    public byte NewsAuthorId { get; set; }
    public bool NewsPublish { get; set; }
    public System.DateTime NewsCreateDate { get; set; }
}

控制器:

public ActionResult Index(Combined model)
{
   var q = _ctx.tblNews.OrderByDescending(x => x.newsCreateDate)
   .Where(x => x.WebsiteID == 2 && x.newsPublish).ToList();

   foreach (var n in q)
   {
       model.NewsList.Add(new NewsList
           {
               //Error: Object reference not set to an instance of an object??? 
               //There is data in the loop from the LINQ query
               NewsId = n.newsID,
               NewsTitle = n.newsTitle,
               NewsStandFirst = n.newsStandFirst,
               NewsCreateDate = n.newsCreateDate
           }
       );
    }
    return View(model);
}

查看:

@model MyMVC.ViewModels.Combined
<div class="row no-margin no-padding">
    <aside class="col-sm-4 col-md-3 col-lg-3">
        @Html.Partial("_PostCode")
    </aside>
    <article class="col-sm-8  col-md-9 col-lg-9 no-margin no-padding">
        <div id="article-inner">
            <h2>News</h2>
            <div class="col-xs-12 col-sm-12 col-md-7 ">
                <ul class="list-group">
                    @foreach (var x in Model.NewsList)
                    {
                        <li class="list-group-item list-group-item-news newslist">
                            @Html.ActionLink(x.NewsTitle, "NewsItem", new { newsId = x.NewsId, newsTitle = x.NewsTitle.ToSeoUrl() })<br />
                            <span class="list-group-item-text">
                                @x.NewsStandFirst
                            </span><br />
                            <span class="text-muted">
                                <small>@x.NewsCreateDate</small>
                            </span>
                        </li>
                    }
                </ul>
            </div>
            <div class="col-xs-12 col-sm-12 col-md-5 ">
            //Some stuff
            </div>
        </div>
    </article>
</div>

PartialView:

@model MyMVC.ViewModels.Order
@using (Html.BeginForm("Prices", "Order"))
{
    <div id="book">
        <h2 class="animated swing">BOOK ONLINE</h2>
        <p class="no-margin no-padding margin-top-10">Please enter your postcode</p>
        <p>
            @Html.TextBoxFor(x => x.Postcode, null, new { @class = "postcode", autofocus = "", autocomplete = "off", maxlength = "8" })
        </p>
        <p><button id="btnSkipPrices" type="submit" class="btn btn-lg btn-primary my-button">Click for a Price <i class="fa fa-chevron-right color-yellow"></i></button></p>
        <p class="no-margin no-padding margin-top-30"><img src="~/Content/img/cards.png" alt="secure credit card payments" class="text-center" /></p>
    </div>
}

我做错了什么?

1 个答案:

答案 0 :(得分:1)

只是&#34;新的&#34;!

public ActionResult Index(Combined model)
{
   var q = _ctx.tblNews.OrderByDescending(x => x.newsCreateDate)
   .Where(x => x.WebsiteID == 2 && x.newsPublish).ToList();

   model.NewsList = new List<NewsList>(); // "new it up"

   foreach (var n in q)
   {
       model.NewsList.Add(new NewsList
           {
               NewsId = n.newsID,
               NewsTitle = n.newsTitle,
               NewsStandFirst = n.newsStandFirst,
               NewsCreateDate = n.newsCreateDate
           }
       );
    }
    return View(model);
}