如何添加两个模型在一个视图ASP.NET MVC [英] How to add two models in one view ASP.NET MVC
本文介绍了如何添加两个模型在一个视图ASP.NET MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个型号。第1模型包含数据库中的每个记录。第二模型包含在记录中的最高金额。
我怎样才能获得两个模型在一个视图?我是新来Asp.NET MVC和只是想获得它的窍门。
下面是code。
型号
命名空间CharitySite.Models
{ 公共类慈善
{
公众诠释ID {搞定;组; }
公共字符串显示名称{搞定;组; }
公众的DateTime日期{搞定;组; }
公共双倍金额{搞定;组; }
公共双TaxBonus {搞定;组; }
公共字符串评论{搞定;组; }
} 公共类CharityDBContext://的DbContext控制了数据库中的信息
{
公益慈善事业最高{集;得到; }
公共DbSet<慈善与GT;捐赠{搞定;组; } //创建一个数据库捐赠
公共CharityDBContext()
{
this.Highest =新慈善();
}
}}
控制器
命名空间CharitySite.Controllers
{
公共类CharitiesController:控制器
{
私人CharityDBContext DB =新CharityDBContext(); // GET:慈善
公众的ActionResult指数()
{
VAR AllDonations =新CharityDBContext();
VAR所有= db.Donations.OrderByDescending(S = GT; s.Amount);
如果(all.Any())
{
//获取最高金额纪录
VAR H = all.First();
AllDonations.Highest.DisplayName = h.DisplayName;
AllDonations.Highest.Amount = h.Amount;
AllDonations.Highest.Comment = h.Comment;
} 返回查看(AllDonations);
}
查看
@model IEnumerable的< CharitySite.Models.Charity>
@model CharitySite.Models.CharityDBContext@ {
ViewBag.Title =指数;}<链接HREF =@ Url.Content(〜/内容/的site.css)的rel =stylesheet属性类型=文/ CSS/>
<链接HREF =@ Url.Content(〜/内容/ bootstrap.css)的rel =stylesheet属性类型=文/ CSS/>
< DIV ID =头> < H1>叙利亚< / H1>< / DIV>< H2>最高和LT; / H>
&所述p为H.; @ Model.Highest.Amount&下; / P>
&所述p为H.; @ Model.Highest.DisplayName&下; / P>< H2>最高和LT; / H>
&所述p为H.; @ Model.Highest.Amount&下; / P>
&所述p为H.; @ Model.Highest.DisplayName&下; / P>< H2>最高和LT; / H>
&所述p为H.; @ Model.Highest.Amount&下; / P>
&所述p为H.; @ Model.Highest.DisplayName&下; / P>< DIV CLASS =表格标题>
@ Html.Partial(_数据库,模型);< / DIV>
解决方案
您可以创建一个包含两个模型的属性自定义类就像这个例子:
公共密封类CharitiesIndexViewModel {
公开名单<慈善与GT;慈善{搞定;组; }
公共最高最高{搞定;组; }
}
后,在您看来,您可以使用它:
@model CharitiesIndexViewModel
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>
解决方案
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
这篇关于如何添加两个模型在一个视图ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文