ComfortablyNumb
ComfortablyNumb

Reputation: 1456

Combining ViewModels and Partial Views

I have a news page that presents a list of latest news items. On the same view I have partial view that has a form. I have combined the two ViewModels in an attempt for both features to work together. However, I'm getting errors.

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; }
}

Controller:

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

View:

@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>
}

What am I doing wrong?

Upvotes: 0

Views: 183

Answers (1)

Todd Sprang
Todd Sprang

Reputation: 2909

Just "new it up"!

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

Upvotes: 1

Related Questions