[英]MVC 3 Razor model not updating: 'Model' does not contain a definition for 'PropertyName'
一個非常簡單的MVC應用程序,具有一個模型,一個松散類型的視圖和控制器,通過ViewBag
將List<Model>
發送到視圖。
在我更新模型之前,一切工作正常。 現在我得到“模型”不包含“ PropertyName”的定義,嘗試重新構建應用程序並清理temp文件夾。
我需要清理什么才能正確重新編譯它?
〜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”不包含“網絡”的定義
嘗試在使用它時將其強制轉換為您的類(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
指令添加到您的名稱空間。
編輯 -抱歉,您剛剛注意到您已經在eduApps.Models
視圖中添加了using
。 我已經修改了答案。
我不明白的是為什么您使用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 Tobi
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.