C#-如何在ASP.NET MVC中显示从数据库到视图的特定记录
好的,我已经创建了我的数据库,它由6行组成,它们是:C#-如何在ASP.NET MVC中显示从数据库到视图的特定记录,c#,mysql,asp.net,asp.net-mvc,asp.net-mvc-4,C#,Mysql,Asp.net,Asp.net Mvc,Asp.net Mvc 4,好的,我已经创建了我的数据库,它由6行组成,它们是: 身份证 显示名 日期 数量 税收红利 评论 现在,这是工作,我使用部分视图来显示它对我的看法。我卡在上面的部分显示了一条特定的记录。我想做的是在同一视图上显示数据库中的三条记录,这些记录与数据库中的enitre记录相同。顶部是这三条记录,然后一个单独的表将包含所有记录。这是因为我想在视图上显示最高捐赠(金额)。首先,即使经过数小时的研究,我也不知道如何显示特定的记录,其次,我不知道如何显示最高记录 下面是帮助你们理解更多的代码: 控制器
- 身份证
- 显示名
- 日期
- 数量
- 税收红利
- 评论
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
}
}
使用系统;
使用System.Collections.Generic;
使用System.Data.Entity;
使用System.Linq;
使用System.Web;
名称空间CharitySite.Models
{
公营慈善机构
{
公共int ID{get;set;}
公共字符串DisplayName{get;set;}
公共日期时间日期{get;set;}
公共双倍金额{get;set;}
公共双TaxBonus{get;set;}
公共字符串注释{get;set;}
}
公共类CharityDBContext:DbContext//控制数据库中的信息
{
公共数据库集捐赠{get;set;}//创建捐赠数据库
}
}
查看
@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>
@model IEnumerable
@{
ViewBag.Title=“Index”;
}
叙利亚
捐赠
@Html.Partial(“_数据库”,模型);
简单来说,我需要的是:
另外,我正在使用Visual Studio Community 2015和ASP.NET以及MVC模板(C#),因此基本上您需要一个具有最高捐赠金额记录和所有捐赠记录属性的视图模型。让我们创建一个
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>();
}
}
现在,您的视图应该是新视图模型的强类型视图
@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>
}
在你的行动方法中,使用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);
在您的视图中,您需要循环查看
最高的
属性,并显示每个项目的金额和注释您需要一个包含两个属性的视图模型,一个用于特定记录,一个用于集合。但你的问题还不清楚。在顶部,您想显示3条记录加上所有记录,然后在底部,您想显示1条记录加上3条记录?谢谢您的帮助。我已经使用了您提供的一些代码,我可以得到最高的部分,因为我已经创建了数据库,并使用部分视图在我的索引页上查看它。现在,我复制了您放置的所有内容,但当我插入@model DonationListVm时,它给我带来了错误-(我将其重命名为“CharityDBContext”。)我得到的错误是,“CS0246:找不到类型或命名空间名称‘CharityDBContext’(您是否缺少using指令或程序集引用?)”好的,我现在已经解决了。我使用了@model CharitySite.Models.DonationListVm,而不是“@model DonationListVm”。这现在可以工作了,但正如我所说的,我想显示最高的捐款,它只显示最高的捐款。有没有办法,我不仅可以添加最高的捐款,还可以添加三个最高的捐款?
public class DonaltionListVm
{
public List<DonationVm> Highest { set;get;}
public List<DonationVm> All { set;get;}
}
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);