这是我的ViewComponent:
public class Categories: ViewComponent
{
private readonly ICategoryService _categories;
public Categories(ICategoryService categories)
{
_categories = categories;
}
public IViewComponentResult Invoke()
{
var cat = _categories.GetCategories();
return View(viewName: "Default", model: cat);
}
}
Run Code Online (Sandbox Code Playgroud)
以下是我在视图中使用它的方法:
@await Component.InvokeAsync("Categories")
Run Code Online (Sandbox Code Playgroud)
我尝试使用缓存:
[ResponseCache(Duration = 99999999)]
Run Code Online (Sandbox Code Playgroud)
上面调用方法但没有工作.
是否可以使用视图组件缓存?如何?
我定义了List这样的:
List<Agahii.Ads> ads = new List<Agahii.Ads>();
Run Code Online (Sandbox Code Playgroud)
然后我想用LINQ查询的结果填充它:
for (int i = 0; i < adid.Count(); i ++ )
{
var dd = adid[i];
var cc = (from a in context.Ads where a.AdID == dd select a).ToList();
ads.Add(cc); // error appears here
};
Run Code Online (Sandbox Code Playgroud)
错误出现在该ads.Add(cc);行中.
错误:System.Collections.Generic.List.Add(Agahii.Ads)的最佳重载方法匹配有一些无效的参数
尝试将数据库(Linq到sql)中的整数数据放入标签中,以获取此错误异常:
赋值的左侧必须是变量属性或索引器
码:
protected void Page_Load(object sender, EventArgs e)
{
DataClassesDataContext data = new DataClassesDataContext();
var visit = (from v in data.SeeSites where v.Date == todaydate select v).FirstOrDefault();
int seennow = visit.See; // On This line I can put data in seenow variable, no problem
Convert.ToInt64(lblSeeNow.Text) = visit.See; // exception error appears here
}
Run Code Online (Sandbox Code Playgroud)