[英]MVC 3 Razor model not updating: 'Model' does not contain a definition for 'PropertyName'
Very simple MVC application, with one model, one loosely typed view and controller sending List<Model>
to the view through ViewBag
. 一个非常简单的MVC应用程序,具有一个模型,一个松散类型的视图和控制器,通过ViewBag
将List<Model>
发送到视图。
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. 现在我得到“模型”不包含“ PropertyName”的定义,尝试重新构建应用程序并清理temp文件夹。
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. 〜Edit :无法找到的属性已添加到未删除的模型中。 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' 错误:“ eduApps.Models.App”不包含“网络”的定义
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 your ViewBag.apps[i]
simply as an object
and not an App
which indeed won't contain any of your defined properties: 尝试在使用它时将其强制转换为您的类(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>
}
}
I don't know where your App
class definition is located so you'll have to replace Namespace.
我不知道您的App
类定义位于何处,因此您必须替换Namespace.
with the appropriate name space, or add a using
directive to your name space at the top of your view. 使用适当的名称空间,或在视图顶部将using
指令添加到您的名称空间。
EDIT - sorry just noticed you've already added a using
to your view of eduApps.Models
. 编辑 -抱歉,您刚刚注意到您已经在eduApps.Models
视图中添加了using
。 I've amended my answer. 我已经修改了答案。
What I don't understand is why u use ViewBag instead of Model. 我不明白的是为什么您使用ViewBag而不是Model。
Your ControllerMethod should look like: 您的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);
}
And your view should do the following; 并且您的视图应执行以下操作;
@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 Hth Tobi
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.