[英]The model item passed into the dictionary is of type 'Umbraco.Web.Models.RenderModel', but this dictionary requires a model item of type 'TestModel'
[英]route hijacking/Cannot bind source type Umbraco.Web.Models.RenderModel to model type
我正在嘗試在umbraco ..中建立強類型視圖,但是我陷在出現此錯誤的地步。
無法將源類型Umbraco.Web.Models.RenderModel綁定到模型類型umbraco_demo.Model.HomeModel。
我的模特班:
public class HomeModel : RenderModel
{
//Standard Model Pass Through
public HomeModel(IPublishedContent content) : base(UmbracoContext.Current.PublishedContentRequest.PublishedContent, UmbracoContext.Current.PublishedContentRequest.Culture) { }
//Custom properties here...
public string MyProperty1 { get; set; }
public string MyProperty2 { get; set; }
}
我的控制器
public class HomeController : Umbraco.Web.Mvc.RenderMvcController
{
public ActionResult HomeModel(RenderModel model)
{
//we will create a custom model
var myCustomModel = new HomeModel(model.Content);
myCustomModel.MyProperty1 = DateTime.Today.ToString();
//TODO: assign some values to the custom model...
return CurrentTemplate(myCustomModel);
}
}
在umbraco中查看:
@using umbraco_demo.Model
@inherits UmbracoViewPage<HomeModel>
@{
Layout = "Master.cshtml";
}
@{Model.Content.GetPropertyValue<string>("MyProperty1");}
另外,我在umbraco中有一個文檔類型,名稱為Home,具有上述模板。
我什至在umbraco論壇上提到了該帖子,但仍然出現相同的錯誤。
將您的控制器方法名稱更改為:
public override ActionResult Index(RenderModel model)
{
...
}
Umbraco.Web.Mvc.RenderMvcController
具有一個也稱為Index
的方法,您需要重寫該方法。 否則,它將使用該虛擬方法作為默認方法(它將返回不同的模型類型)。
Index
也是在沒有(GET或POST)參數的情況下加載頁面時將調用的默認方法。
如果在控制器上使用了調試器,則應該能夠看到自定義方法未在頁面加載時被擊中。
我沒有使用您對模型的特定實現。 可能值得將構造函數更改為:
public HomeModel(IPublishedContent content, CultureInfo culture) : base (content, culture) { }
並將此模型的控制器中的實例化為:
var myCustomModel = new HomeModel(model.Content, System.Globalization.CultureInfo.CurrentCulture);
您試圖獲取屬性MyProperty1
視圖中的那一行可能是錯誤的。 我假設此屬性在您的Umbraco節點上不存在,但是您的意思是在自定義模型上訪問該屬性。
更改:
@{Model.Content.GetPropertyValue<string>("MyProperty1");}
至:
@{var myProperty1 = Model.MyProperty1;}
另外,請確保您的控制器名稱與您當前使用的文檔類型相同
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.