2017-03-17 13 views
0

我現在感覺非常愚蠢。我有一個強制鍵入列表的視圖,但我也想顯示上次更新列表的日期。使用列表和單個變量返回視圖

控制器

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

這是我想要顯示的日期代碼(我試過裏面ListCustLedgerEntries加入,但我知道那是錯誤的)。

 var datetoweb = db.CustLedgerEntries.FirstOrDefault(m => m.DatetoWebSales != null); 

這是我的看法

@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 

就像我說的,我知道這應該很容易。任何幫助做這個和任何教程比我發現更好將不勝感激。

回答

1

有不同的方法來做到這一點,一個是創建具有2個屬性,記錄和LastUpdatedDate

public classs AccountHistoryVm 
{ 
    public IEnumerable<CustLedgerEntry> Records { set;get;} 
    public DateTime? LastUpdatedDate { set;get;} 
} 
在GET行動

現在,一個新視圖模型,創建這樣的一個對象,設置相關的性能和發送,查看

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

確保您的看法,現在是強類型的這種新的視圖模型

@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 

另一種選擇是使用ViewBag傳遞數據。

+0

我試過ViewBag,但是我得到下面的結果「這是Infotech.Coverpools.Portal.Tintaglia.CodeFirst.Models.CustLedgerEntry」的準確性而不是日期。我在我的視圖中添加了'ViewBag.datetoweb = db.CustLedgerEntries.FirstOrDefault(m => m.DatetoWebSales!= null);'ListCustLegerEntries和'@ ViewBag.datetoweb'。我會嘗試VM解決方案,但爲什麼我會得到這個結果? –

+0

謝謝。視圖模型工作。仍然不確定ViewBag爲什麼返回路徑而不是數據。我沒有'var datetoweb = db.CustLedgerEntries.FirstOrDefault(m => m.DatetoWebSales!= null); vm.LastUpdatedDate = datetoweb.DatetoWebSales;'和'@ Model.LastUpdateDate'而不是id檢查null。 –

相關問題