I”m working in MVC 3 with Razor views. I’ve created a page that calls a partial view
@Html.Partial("_FeaturedRatesBox")
which looks like this:
@model IEnumerable<AppleWeb.Models.RateViewer>
<div class="col">
<h3>Today's Featured Rates</h3>
<form method="post" action="">
<div class="tabber">
<ul class="tabs unstyled">
<li class="selected"><a href="#tab1">Loans</a></li>
<li><a href="#tab2">Deposits</a></li>
</ul>
<div class="tab" id="tab1">
@foreach (var item in Model)
{
<div class="rate"> <span class="left">@item.AccountName <br />
</span> <span class="right"> As low as <span class="percent">@item.Rate</span></span></div>
<!-- end of rate -->
}
</div>
<!-- end of tab -->
<div class="tab" id="tab2"> Loremipsum
</div>
<!-- end of tab -->
<button type="submit" class="btn alignright">Apply Now</button>
</div>
<!-- end of tabber -->
</form>
</div>
and the rateviewer.cs model looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.EntityModel;
using AppleWeb.Models;
namespace AppleWeb.Models
{
public class RateViewer
{
private PublicWebEntities pubDB = new PublicWebEntities();
public virtual IEnumerable<DepositRate> FeaturedRatesDeposit()
{
var data = from item in pubDB.DepositRates
where item.Featured.Equals(true)
select item;
return data.ToList();
}
}
}
It errors out on the line calling the partial view and says
CS1061: ‘AppleWeb.Models.RateViewer’ does not contain a definition for ‘AccountName’ and no extension method ‘AccountName’ accepting a first argument of type ‘AppleWeb.Models.RateViewer’ could be found (are you missing a using directive or an assembly
Well AccountName and Rate are both entity names for the DepositRates Entity Object. From the DataModel.Designer file:
public static DepositRate CreateDepositRate(global::System.Int32 rateID, global::System.String accountName, global::System.Double rate, global::System.Double aPY, global::System.Int16 sortingOrder)
{
DepositRate depositRate = new DepositRate();
depositRate.RateID = rateID;
depositRate.AccountName = accountName;
depositRate.Rate = rate;
depositRate.APY = aPY;
depositRate.SortingOrder = sortingOrder;
return depositRate;
}
I’d hope to make this work as such, because then I can use this model elsewhere on the site.
You need to iterate the IEnumerable property on your Model ie.
@foreach (var item in Model.FeaturedRatesDeposit)