[英]Partial View and Model not working
I"m working in MVC 3 with Razor views. I've created a page that calls a partial view 我正在使用Razor视图在MVC 3中工作。我创建了一个调用局部视图的页面
@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: 而rateviewer.cs
模型如下所示:
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 CS1061:'AppleWeb.Models.RateViewer'不包含'AccountName'的定义,并且没有可以找到接受“AppleWeb.Models.RateViewer”类型的第一个参数的扩展方法'AccountName'(您是否缺少using指令或部件
Well AccountName and Rate are both entity names for the DepositRates Entity Object. 好AccountName和Rate都是DepositRates实体对象的实体名称。 From the DataModel.Designer file: 从DataModel.Designer文件:
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. 您需要在模型上迭代IEnumerable属性,即。 @foreach (var item in Model.FeaturedRatesDeposit)
You need to pass an object which is an IEnumerable of type AppleWeb.Models.RateViewer to the view, 您需要将一个IEnumerable类型的对象AppleWeb.Models.RateViewer传递给视图,
so the code should look like: 所以代码应如下所示:
@Html.Partial("_FeaturedRatesBox", new List<AppleWeb.Models.RateViewer>())
"new List<AppleWeb.Models.RateViewer>()" can be replaced with the actual instance of the collection you want to pass to the view.
Also, the type "RateViewer" does not contain any field called "AccountName". 此外,“RateViewer”类型不包含任何名为“AccountName”的字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.