2016-02-29 66 views
1

行,所以我创造了我的数据库,其中包括6行分别是:C# - 如何显示从数据库中的特定记录在ASP.NET MVC视图

  • ID
  • 显示名称
  • 日期
  • 金额
  • TaxBonus
  • 评论

现在,这是工作,我用部分视图来展示我的看法。我卡在的部分正在显示特定的记录。我想要做的是在数据库中显示来自我的数据库的三条记录。顶部将是这三条记录,然后一个单独的表将包含所有记录。原因是因为我想在视图上显示最高的捐款(金额)。我首先不知道如何在经过数小时的研究后再显示特定的记录,然后显示出高的记录。

下面是代码,这将有助于你们了解更多:

控制器

using System; 
using System.Collections.Generic; 
using System.Data; 
using System.Data.Entity; 
using System.Linq; 
using System.Net; 
using System.Web; 
using System.Web.Mvc; 
using CharitySite.Models; 

namespace CharitySite.Controllers 
{ 
    public class CharitiesController : Controller 
    { 
     private CharityDBContext db = new CharityDBContext(); 

     // GET: Charities 
     public ActionResult Index() 
     { 
      return View(db.Donations.ToList()); 
     } 

型号

using System; 
using System.Collections.Generic; 
using System.Data.Entity; 
using System.Linq; 
using System.Web; 

namespace CharitySite.Models 
{ 
    public class Charity 
    { 
     public int ID { get; set; } 
     public string DisplayName { get; set; } 
     public DateTime Date { get; set; } 
     public Double Amount { get; set; } 
     public Double TaxBonus { get; set; } 
     public String Comment { get; set; } 
    } 

    public class CharityDBContext : DbContext //controls information in database 
    { 
     public DbSet<Charity> Donations { get; set; } //creates a donation database 
    } 
} 

查看

@model IEnumerable<CharitySite.Models.Charity> 

@{ 
    ViewBag.Title = "Index"; 

} 


<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" /> 
<link href="@Url.Content("~/Content/bootstrap.css")" rel="stylesheet" type="text/css" /> 
<div id="header"> 

    <h1>Syria</h1> 
    <h4>DONATIONS</h4> 

</div> 

<div class="half-col"> 
    @Html.Partial("_Database", Model); 

</div> 

所以简单来说什么,我需要的是:

  1. 在视图中显示特定记录
  2. 显示最高捐款(金额) - 他们的名称和注释在视图

研究无处不在,但我找不到任何东西,为什么我来这里问,如果你们任何人都知道解决方案,使这项工作。

P.S - 我使用Visual Studio 2015年社区与ASP.NET和MVC模板(C#)

+0

您需要一个包含2个属性,一个特定的记录,一个用于收集一个视图模型。但你的问题还不清楚。在顶部,你想显示3条记录加上所有记录,然后在底部,你想显示1条记录加3条记录? –

回答

1

所以基本上你需要它具有最高的捐赠金额纪录,所有的捐赠记录的属性视图模型。所以我们来创建一个。

public class DonationVm 
{ 
    public decimal Amount { set;get;} 
    public string DisplayName { set;get;} 
    public string Comment { set;get;} 
} 
public class DonationListVm 
{ 
    public DonationVm Highest { set;get;} 
    public List<DonationVm> All { set;get;} 
    public DonationListVm() 
    { 
    this.Highest = new DonationVm(); 
    this.All = new List<DonationVm>(); 
    } 
} 

而在你的GET操作方法,创建用于这两种性质的DonationListVm对象,负载数据和传递给视图。您可以通过Amount属性进行降序排列,并以第一条记录获取最高金额的记录。

public ActionResult Index() 
{ 
    var vm=new DonationListVm(); 
    var all =db.Donations.OrderByDescending(s=>s.Amount); 
    if(all.Any()) 
    { 
    //Get the highest amount record 
     var h = all.First(); 
     vm.Highest.Name=h.Name; 
     vm.Highest.Amount = h.Amount; 
     vm.Highest.Comment = h.Comment; 
    } 
    //Get all the records 
    vm.All=all.Select(x=>new DonationVm { Amount=x.Amount, 
           Comment=x.Comment, 
           DisplayName =x.DisplayName}).ToList(); 
    return View(vm); 
} 

现在你的观点应该是强类型到我们新的视图模型

@model DonationListVm 
<h2>Highest</h2> 
<p>@Model.Highest.Amount</p> 
<p>@Model.Highest.DisplayName</p> 

<h3>All donations</h3> 
@foreach(var item in Model.All) 
{ 
    <p>@item.DisplayName - @item.Amount</p> 
} 

如果你想显示3最高捐款,而不只是一个,你DonationListVmHighest属性更改为一个集合

public class DonaltionListVm 
{ 
    public List<DonationVm> Highest { set;get;} 
    public List<DonationVm> All { set;get;} 
} 

并在您的操作方法中使用Take方法获取3个项目。

var vm=new DonationListVm(); 
vm.All=db.Donations.Select(x=>new DonationVm { Amount =x.Amount, 
       Comment =x.Comment,DisplayName=x.DisplayName }).ToList(); 
//sort based on amount then take top 3 records 
vm.Highest=cm.All.OrderByDescending(y=>y.Amount).Take(3) 
      .Select(a=>new DonationVm { Amount=a.Amount, 
             Comment=a.Comment, 
             DisplayName=a.DisplayName}).ToList(); 
return View(vm); 

,并在你看来,你需要通过Highest财产循环,并显示每个项目的金额和评论

+0

谢谢你的帮助。我已经使用了部分代码,以及我可以获得最高的部分,因为我已经创建了数据库并使用部分视图在我的索引页上查看它。现在我复制了你放的所有东西,但是当我插入@ model DonationListVm时它给了我和错误 - (我将它重命名为“CharityDBContext”。)我得到的错误是,“CS0246:类型或命名空间名称'CharityDBContext'不能发现(你是否缺少使用指令或程序集引用?)“ –

+0

好吧,我已经解决了它。而不是“@ model DonationListVm我用”模型CharitySite.Models.DonationListVm“。这现在工作,但正如我所说我想显示最高的捐赠,它只显示最高。有没有什么办法,我可以添加不只是最高但是最高的三笔捐款? –

+0

@ ZerghamChoudhary查看最新的答案。 – Shyju

相关问题