【问题标题】:C# - How to Display a specific record from a database to a view in ASP.NET MVCC# - 如何在 ASP.NET MVC 中将特定记录从数据库显示到视图
【发布时间】:2016-02-29 01:39:27
【问题描述】:

好的,所以我创建了包含 6 行的数据库:

  • 身份证
  • 显示名称
  • 日期
  • 金额
  • 税收奖励
  • 评论

现在这正在工作,我使用局部视图将其显示在我的视图中。我坚持的部分是显示特定记录。我想做的是在与数据库中的全部记录相同的视图上显示我的数据库中的三个记录。顶部将是这三个记录,然后一个单独的表将包含所有记录。这样做的原因是因为我想在视图上显示最高的捐赠(金额)。首先,即使经过数小时的研究,我也不知道如何显示特定记录,其次是显示最高记录。

下面是帮助大家理解的代码:

控制器

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 - 我正在使用带有 ASP.NET 和 MVC 模板(C#)的 Visual Studio Community 2015

【问题讨论】:

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

标签: c# mysql asp.net asp.net-mvc asp.net-mvc-4


【解决方案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 属性并显示每个项目的金额和评论

【讨论】:

  • 感谢您的帮助。我已经使用了您给出的一些代码,以及我可以获得最高的部分,因为我已经创建了数据库并使用部分视图在我的索引页面上查看它。现在我复制了你放的所有东西,但是当我插入@模型 DonationListVm 时它给了我和错误 - (我将它重命名为“CharityDBContext”。)我得到的错误是,“CS0246:类型或命名空间名称'CharityDBContext'不能是找到(您是否缺少 using 指令或程序集引用?)"
  • 好的,我现在已经解决了。而不是 "@model DonationListVm 我使用 "@model CharitySite.Models.DonationListVm" 。这现在有效,但正如我所说,我想显示最高的捐款,它只显示最高的。有什么方法可以添加不只是最高但最高的三笔捐款?
猜你喜欢
  • 2014-10-06
  • 2020-03-30
  • 2014-11-15
  • 1970-01-01
  • 2010-09-22
  • 1970-01-01
  • 2021-06-10
  • 2013-09-21
  • 2020-01-29
相关资源
最近更新 更多