Display Data Through ViewModel in ASP.Net MVC

Controller:

private MyDbContext db = new MyDbContext();

// GET: TransactionCAs
public ActionResult Index()
{
var TVM = (from t in db.MyTransactionCA
join a in db.MyAgency on t.Agency_Id equals a.AgencyId
select new TransVM
{
Agency_Name = a.AgencyName,
TransactionCAId = t.TransactionCAId,
PBusinessSolution = t.PBusinessSolution,
MonetaryCost = t.MonetaryCost,
MonetoryBenefit = t.MonetoryBenefit,
LaborCost = t.LaborCost,
LaborBenefite =t.LaborBenefite,
OtherCost = t.OtherCost,
OtherBenefite = t.OtherBenefite,

}
);
return View(“Index”,TVM);
}

 

 

Main Model:

public class TransactionCA:BaseEntity
{
[Key]
public int TransactionCAId { get; set; }
[Required]
[DisplayName(“Proposed Business Solution”)]
public string PBusinessSolution { get; set; }
/*——-*/
[Required]
[DisplayName(“Monetary Cost”)]
public decimal MonetaryCost { get; set; }
/*——-*/
[Required]
[Display(Name = “Monetory Benefite”)]
public decimal MonetoryBenefit { get; set; }
/*——-*/
[Required]
[Display(Name = “Labor Cost”)]
public decimal LaborCost { get; set; }
/*——-*/
[Required]
[Display(Name = “Labor Benefite”)]
public decimal LaborBenefite { get; set; }
/*——-*/
[Required]
[Display(Name = “Other Cost”)]
public decimal OtherCost { get; set; }
/*——-*/
[Required]
[Display(Name = “Other Benefit”)]
public decimal OtherBenefite { get; set; }
[DisplayName(“Total Cost”)]
public decimal TotalCost { get; set; }
[DisplayName(“Total Benefit”)]
public decimal TotalBenefit { get; set; }
[DisplayName(“Priority Ratio”)]
public decimal PriorityRatio { get; set; }

//[DisplayName(“Agency”)]
public int Agency_Id { get; set; }
//public virtual Agency Agency {get; set;}

}

 

//—————————–//

View Model

public class TransVM
{
public int TransVMId { get; set; }
public string Agency_Name { get; set; }

/*—-Table Trans——-*/
public int TransactionCAId { get; set; }
public string PBusinessSolution { get; set; }

public decimal MonetaryCost { get; set; }
/*——-*/
[Display(Name = “Monetory Benefite”)]
public decimal MonetoryBenefit { get; set; }
/*——-*/
[Display(Name = “Labor Cost”)]
public decimal LaborCost { get; set; }
/*——-*/
[Display(Name = “Labor Benefite”)]
public decimal LaborBenefite { get; set; }
/*——-*/
[Display(Name = “Other Cost”)]
public decimal OtherCost { get; set; }
/*——-*/
[Display(Name = “Other Benefit”)]
public decimal OtherBenefite { get; set; }
[Display(Name=”Total Cost”)]
public decimal TotalCost { get; set; }
[Display(Name=”Total Benefit”)]
public decimal TotalBenefit { get; set; }
[Display(Name=”Priority Ratio”)]
public decimal PriorityRatio { get; set; }
}

//———————-View—————–//

@model IEnumerable<CAuth.ViewModels.TransVM>

@{
ViewBag.Title = “Index”;
}

<h2>Index</h2>

<p>
@Html.ActionLink(“Create New”, “Create”)
</p>
<table class=”table”>
<tr>
<th>
Agency
</th>
<th>
@Html.DisplayNameFor(model => model.PBusinessSolution)
</th>
<th>
@Html.DisplayNameFor(model => model.MonetaryCost)
</th>
<th>
@Html.DisplayNameFor(model => model.MonetoryBenefit)
</th>
<th>
@Html.DisplayNameFor(model => model.LaborCost)
</th>
<th>
@Html.DisplayNameFor(model => model.LaborBenefite)
</th>
<th>
@Html.DisplayNameFor(model => model.OtherCost)
</th>
<th>
@Html.DisplayNameFor(model => model.OtherBenefite)
</th>
<th></th>
</tr>

@foreach (var item in Model) {
<tr>
<td>
@item.Agency_Name
</td>
<td>
@item.PBusinessSolution
</td>
<td>@item.MonetaryCost</td>
<td>@item.MonetoryBenefit</td>
<td>@item.LaborCost</td>
<td>@item.LaborBenefite</td>
<td>@item.OtherCost</td>
<td>@item.OtherBenefite</td>

</tr>
}

</table>

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.