非常简单的 MVC 应用程序,具有一个模型、一个松散类型 View 和发送 Controller
List<Model>
通过ViewBag
查看.
在我更新模型之前一切都工作正常。现在我得到“模型”不包含“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; }
}
Controller :
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”的定义
最佳答案
使用时尝试转换为您的类(ViewBag.apps[i] as eduApps.Models.App)
,因为此处没有强类型,MVC 将看到您的ViewBag.apps [i]
只是一个对象
,而不是一个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.
替换为适当的 namespace ,或添加 using
指令位于 View 顶部的命名空间。
编辑 - 抱歉,刚刚注意到您已经将 using
添加到 eduApps.Models
View 中。我修改了我的答案。
关于c# - MVC 3 Razor 模型未更新 : 'Model' does not contain a definition for 'PropertyName' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13684856/