c# - MVC 3 Razor 模型未更新 : 'Model' does not contain a definition for 'PropertyName'

标签 c# asp.net-mvc-3 razor model

非常简单的 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/

相关文章:

c# - 使用 IsDefined 而不是 GetCustomAttributes 有好处吗

asp.net-mvc-3 - MVC3 MapRoute,如何

c# - 从 Razor 调用 C# 助手时是否会牺牲性能?

c# - 从 asp.net mvc 中的数据库表填充单选按钮列表

c# - Entity Framework + ASP.NET Identity + Repository Pattern - 如何在加载用户的同时加载角色

c# - 如何从 Windows 窗体打印明文?

jquery - 从 MVC3 中的 Controller 向 AJAX 调用返回错误

asp.net-mvc - 表单 enctype ="multipart/form-data"不工作

visual-studio-2010 - 如何在 Visual Studio 2010 中编辑默认的 Razor Creat/编辑脚手架

c# - 什么时候配置 Log4Net?