-1

I have two models. The 1st model contains every record in the database. The 2nd model contains the highest amount in the records.

How can I get two models in one view? I am new to Asp.NET MVC and just trying to get the hang of it.

Here is the code.

Model

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 Charity Highest { set; get; }
        public DbSet<Charity> Donations { get; set; } //creates a donation database
        public CharityDBContext()
        {
            this.Highest = new Charity();


        }
    }

}

Controller

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

        // GET: Charities
        public ActionResult Index()
        {
            var AllDonations = new CharityDBContext();
            var all = db.Donations.OrderByDescending(s => s.Amount);
            if (all.Any())
            {
                //Get the highest amount record
                var h = all.First();
                AllDonations.Highest.DisplayName = h.DisplayName;
                AllDonations.Highest.Amount = h.Amount;
                AllDonations.Highest.Comment = h.Comment;
            }

            return View(AllDonations);
        }

View

@model IEnumerable<CharitySite.Models.Charity>
@model CharitySite.Models.CharityDBContext



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

</div>

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

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

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

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

</div>
Filburt
  • 17,626
  • 12
  • 64
  • 115
Edafy
  • 31
  • 2
  • 10
  • 1
    Why not add a class which contains Charity and CharityDBContext? – Werner Feb 29 '16 at 14:57
  • 1
    the pattern doesnt work that way. use the viewbag or create a model that has your two "models" as properties – Thorarins Feb 29 '16 at 14:57
  • 1
    You're thinking about it incorrectly. A model's purpose is to provide a structure that can be passed to and from a view. It's basically there to serve the UI (in a roundabout way). If you have 2 models for one view, the view's requirements aren't being fulfilled. Create another model with properties that expose what your view needs. –  Feb 29 '16 at 15:00
  • You're using EF DbContext incorrectly. It should not be used as a model. You get information from DbContext and insert it into the model, you should never be providing a DbContext to your view. – mason Feb 29 '16 at 15:01
  • Possible duplicate of [What is ViewModel in MVC?](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) – rogerdeuce Feb 29 '16 at 15:39

1 Answers1

2

You can create a custom class with two model's properties like in this example:

public sealed class CharitiesIndexViewModel {
     public List<Charity> Charities { get; set; }
     public Highest Highest { get; set; }
}

After, in your View, you can use it:

@model CharitiesIndexViewModel
Roberto Conte Rosito
  • 2,080
  • 12
  • 22
  • Thanks for the answer. Where did you get the public highest from ? Should it not be public CharityDBContext? Even if i put this in i still get errors. The error does occur in your code given but it gives an error in my view where i try to use the @ model CharitiesIndexViewModel – Edafy Feb 29 '16 at 15:22
  • I don't know if Highest is correct, I just provide you an example. Of course your model needs to be public. Can you be more specific? – Roberto Conte Rosito Feb 29 '16 at 17:18