非常简单的MVC应用程序,有一个模型,一个松散类型的视图和控制器发送
通过List<Model>
查看ViewBag
视图。
所有工作正常,直到我更新模型。现在我得到'Model'不包含'PropertyName'的定义,尝试重建应用程序并清理临时文件夹。
我需要清理什么才能正确地重新编译?
〜编辑:无法找到的属性已添加到未删除的模型中。我正试图在View中使用这个新属性。
〜修改: 型号:
public class App
{
public String title { get; set; }
public String featured { get; set; }
public String subtitle { get; set; }
public String thumb { get; set; }
public String logo { get; set; }
public String web { get; set; }
public String email { get; set; }
public String phone { get; set; }
public String download { get; set; }
public String body { get; set; }
public List<String> tags { get; set; }
public List<String> features { get; set; }
public List<Highlight> highlights { get; set; }
}
控制器:
ViewBag.apps = (from xml in XmlResources.Root.Descendants("app")
select new App
{
title = xml.Element("title").Value,
featured = xml.Attribute("featured").Value,
subtitle = xml.Element("subtile").Value,
thumb = xml.Element("thumb").Value,
logo = xml.Element("logo").Value,
web = xml.Element("web").Value,
email = xml.Element("email").Value,
phone = xml.Element("phone").Value,
download = xml.Element("download").Value,
body = xml.Element("body").Value,
tags = (from x in xml.Descendants("tag") select x.Value).ToList(),
features = (from x in xml.Descendants("feature") select x.Value).ToList(),
highlights = (from x in xml.Descendants("highlight") select new Highlight { type = x.Attribute("type").Value, src = x.Attribute("src").Value }).ToList()
}).ToList();
查看:
@using eduApps.Models;
@for (var i = 0; i < ViewBag.apps.Count; i++)
{
@{ if(!String.IsNullOrEmpty(ViewBag.apps[i].web))
{
<span>Web:</span><a href="@ViewBag.apps[i].web" title="@ViewBag.apps[i].title">@ViewBag.apps[i].web</a>
}
}
}
错误: 'eduApps.Models.App'不包含'web'的定义
答案 0 :(得分:3)
在使用时尝试投放到您的班级(ViewBag.apps[i] as eduApps.Models.App)
,因为此处没有强力输入MVC会将您的ViewBag.apps[i]
视为object
,而不是App
确实不会包含任何已定义的属性:
@{
if(!String.IsNullOrEmpty((ViewBag.apps[i] as App).web))
{
<span>Web:</span><a href="@(ViewBag.apps[i] as App).web" title="@(ViewBag.apps[i] as App).title">@(ViewBag.apps[i] as App).web</a>
}
}
我不知道您的App
类定义位于何处,因此您必须使用适当的名称空间替换Namespace.
,或添加using
指令到视图顶部的名称空间。
编辑 - 抱歉,您已经注意到您已经在using
的视图中添加了eduApps.Models
。我修改了我的答案。
答案 1 :(得分:2)
我不明白为什么你使用ViewBag而不是Model。
您的ControllerMethod应如下所示:
public ActionResult Foo(){
IList<App> model =
(from xml in XmlResources.Root.Descendants("app")
select new App
{
title = xml.Element("title").Value,
featured = xml.Attribute("featured").Value,
subtitle = xml.Element("subtile").Value,
thumb = xml.Element("thumb").Value,
logo = xml.Element("logo").Value,
web = xml.Element("web").Value,
email = xml.Element("email").Value,
phone = xml.Element("phone").Value,
download = xml.Element("download").Value,
body = xml.Element("body").Value,
tags = (from x in xml.Descendants("tag") select x.Value).ToList(),
features = (from x in xml.Descendants("feature") select x.Value).ToList(),
highlights = (from x in xml.Descendants("highlight") select new Highlight { type = x.Attribute("type").Value, src = x.Attribute("src").Value }).ToList()
}).ToList();
return View(model);
}
您的观点应该做到以下几点;
@model IList<App>
@foreach(App app in Model){
@{ if(!String.IsNullOrEmpty(app.web))
{
<span>Web:</span><a href="@app.web" title="@app.title">@app.web</a>
}
}
}
Hth Tobi