简体   繁体   中英

C# - How to Display a specific record from a database to a view in ASP.NET MVC

Ok so i have created my database which consist of 6 rows which are:

  • ID
  • DisplayName
  • Date
  • Amount
  • TaxBonus
  • Comment

Now this is working and i use partial view to dislay it on to my view. The part i am stuck on is displaying a specific record. What i would like to do is display three record from my database on the the same view as my enitre records from database. At the top will be these three records then a seperate table will have all the records. Reason for this is because i want display the highest donation(amount) on to the view. I firstly have no idea how to display a specific record even after hours of research and secondly displaying the highes record.

Below is the code which will help you guys understand more:

Controller

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

Model

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

View

@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>

So in simple terms what i need is to:

  1. Display a specific record in a view
  2. Display the highest donations (amount) - with their name and comment in a view

Researched everywhere to do this but i could not find anything, reason why i have come here to ask if any of you guys know the solution to make this work.

PS - I am using Visual Studio Community 2015 with ASP.NET and the MVC template(C#)

So basically you need a view model which has properties for the highest donation amount record and all the donation records. So let's create one.

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

And in your GET action method, create an object of DonationListVm , load data for both the properties and pass to the view. You can do a descending order by on the Amount property and take the first record to get the record with highest 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);
}

Now your view should be strongly typed to our new view model

@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>
}

If you want to show 3 highest donation instead of just one,change the Highest property of your DonationListVm to a collection

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

and in your action method get the 3 items using Take method.

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

and in your view, you need to loop through the Highest property and show each items's Amount and Comment

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM