Reputation: 75
I'm feeling quite dumb right now. I have a view that is strongly typed to a list, but I also want to show a date when this list was last updated.
Controller
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Infotech.Coverpools.Portal.Tintaglia.CodeFirst.Models;
namespace Infotech.Coverpools.Portal.Tintaglia.Web.Controllers
{
public class CustLedgerEntryController : Controller
{
private TintagliaContext db = new TintagliaContext();
//
// GET: /CustLedgerEntry/
public ActionResult AccountHistory()
{
//UserProfile userprofile = db.UserProfiles.FirstOrDefault(u => u.UserName.Equals(User.Identity.Name));
IEnumerable<CustLedgerEntry> ListCustLedgerEntries;
{
ListCustLedgerEntries = db.CustLedgerEntries.ToList();
}
return View(ListCustLedgerEntries);
}
This is the code for the date I want to display (which I tried adding inside ListCustLedgerEntries, but I knew that was wrong).
var datetoweb = db.CustLedgerEntries.FirstOrDefault(m => m.DatetoWebSales != null);
Here's my view
@model IEnumerable<Infotech.Coverpools.Portal.Tintaglia.CodeFirst.Models.CustLedgerEntry>
@using GridMvc.Html
@{
ViewBag.Title = "Account History";
}
<h2>Account History</h2>
@*<link href="~/Content/Gridmvc.css" rel="stylesheet" />
<link href="~/Content/bootstrap.min.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.9.1.min.js"></script>*@
@Html.Grid(Model).Columns(columns =>
{
columns.Add(m => m.CustomerNo_).Titled("Account #").Filterable(true);
columns.Add(m => m.PostingDate).Format("{0:dd/MM/yyyy}").SetWidth(100).Titled("Order Date").Filterable(true);
columns.Add(m => m.DocumentType).Titled("Type").Filterable(true);
columns.Add(m => m.DocumentNo_).Titled("Document #").Filterable(true);
columns.Add(m => m.Description).Titled("Description").SetWidth(300).Filterable(true);
columns.Add(m => m.ExternalDocumentNo_).Titled("Memo").SetWidth(300).Filterable(true);
columns.Add(m => m.Amount).Format("{0:$#,###.00}").Titled("Amount").SetWidth(200).Filterable(true);
columns.Add(m => m.RemainingAmount).Format("{0:$#,###.00}").SetWidth(100).Titled("Remaining Amount").Filterable(true);
columns.Add(m => m.DueDate).Format("{0:dd/MM/yyyy}").Titled("Due Date").Filterable(true);
}).WithPaging(20).Sortable(true)
This is accurate as of //This is where I want the Date
Like I said, I know this should be easy. Any help in doing this and any tutorials better than I've found would be greatly appreciated.
Upvotes: 0
Views: 479
Reputation: 218782
There are different ways to do it, One is to create a new view model which has 2 properties, Records and LastUpdatedDate
public classs AccountHistoryVm
{
public IEnumerable<CustLedgerEntry> Records { set;get;}
public DateTime? LastUpdatedDate { set;get;}
}
Now in your GET action, create an object of this, set the relevant properties and send to view
public ActionResult AccountHistory()
{
var vm = new AccountHistoryVm();
vm.Records= db.CustLedgerEntries.ToList();
var d= db.CustLedgerEntries.FirstOrDefault(m => m.DatetoWebSales != null);
id(d!=null)
{
vm.LastUpdateDate == d.DatetoWebSales; //Assuming DatetoWebSales is of DateTime type.
}
return View(vm);
}
Make sure your view is now strongly typed to this new view model
@model AccountHistoryVm
@if(Model.LastUpdatedDate!=null)
{
<p>This is accurate as of @Model.LastUpdatedDate.Value.ToString() </p>
}
// Use Model.Records property to build the grid
Another option is to use ViewBag to pass the data.
Upvotes: 1