2

非常简单的 MVC 应用程序,一个模型,一个松散类型 List<Model>的视图和控制器通过ViewBag.

在我更新模型之前一切正常。现在我得到“模型”不包含“属性名称”的定义,尝试重建应用程序并清理临时文件夹。

我需要清理什么才能正确重新编译?

~Edit:无法找到的属性已添加到未删除的模型中。我正在尝试在视图中使用这个新属性。

~编辑:型号:

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”的定义

4

2 回答 2

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在视图顶部的名称空间中添加一个指令。

编辑- 抱歉刚刚注意到您已经usingeduApps.Models. 我已经修改了我的答案。

于 2012-12-03T14:13:34.023 回答
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 托比

于 2012-12-03T14:42:31.443 回答