Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 返回带有列表和单个变量的视图_Asp.net Mvc_Linq To Sql - Fatal编程技术网

Asp.net mvc 返回带有列表和单个变量的视图

Asp.net mvc 返回带有列表和单个变量的视图,asp.net-mvc,linq-to-sql,Asp.net Mvc,Linq To Sql,我现在觉得自己很笨。我有一个强类型为列表的视图,但我还想显示此列表上次更新的日期 控制器 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.

我现在觉得自己很笨。我有一个强类型为列表的视图,但我还想显示此列表上次更新的日期

控制器

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);
        }
这是我的看法

@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

就像我说的,我知道这应该很容易。如果您能提供比我发现的更好的帮助和教程,我们将不胜感激。

有不同的方法可以做到这一点,一种是创建一个新的视图模型,它有两个属性:记录和LastUpdateDate

public classs AccountHistoryVm
{
  public IEnumerable<CustLedgerEntry> Records { set;get;}
  public DateTime? LastUpdatedDate { set;get;}
}
确保您的视图现在已强类型化到此新视图模型

@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传递数据。

我尝试了ViewBag,但得到了以下结果:这是从Infotech.Coverpools.Portal.Tintaglia.CodeFirst.Models.CustLedgerEntry开始的准确结果,而不是日期。我添加了ViewBag.datetoweb=db.CustLedgerEntries.FirstOrDefaultm=>m.DatetoWebSales!=无效的在我的视图中的ListCustomerEntries和@ViewBag.datetoweb中。我会尝试虚拟机解决方案,但为什么我会得到这个结果?谢谢。视图模型工作正常。仍然不确定ViewBag返回路径而不是数据的原因。我做了var datetoweb=db.CustLedgerEntries.FirstOrDefaultm=>m.DatetoWebSales!=无效的vm.lastUpdateDate=datetoweb.DatetoWebSales;和@Model.LastUpdateDate,而不是检查null的id。
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