Very simple MVC application, with one model, one loosely typed view and controller sending
List<Model> to the view through ViewBag.
All was working fine until I updated the model. Now I get ‘Model’ does not contain a definition for ‘PropertyName’, Tried rebuilding the application and cleaning the temp folder.
What do I need to clean up to get this recompiled properly?
~Edit: Property that cannot be found was added to the Model not removed. And I’m trying to use this new property in the View.
~Edit:
Model:
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();
View:
@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>
}
}
}
Error:
‘eduApps.Models.App’ does not contain a definition for ‘web’
Try casting to your class
(ViewBag.apps[i] as eduApps.Models.App)when using it, as there is no strong typing here MVC will see yourViewBag.apps[i]simply as anobjectand not anAppwhich indeed won’t contain any of your defined properties:I don’t know where your
Appclass definition is located so you’ll have to replaceNamespace.with the appropriate name space, or add ausingdirective to your name space at the top of your view.EDIT – sorry just noticed you’ve already added a
usingto your view ofeduApps.Models. I’ve amended my answer.