所以,我試圖找到Umbraco節點(如iPublishedContent),並將其傳遞給viewModel(如Ш've劫持了一條路線)。所以我把這個在我的控制器:System.NullReferenceException創建視圖模型
private AddCouponCodesViewModel viewModel;
public AddCouponCodesController(){
//Get iPublished content
IPublishedContent content = Umbraco.TypedContent(1225);
//Pass to viewModel
viewModel = new AddCouponCodesViewModel(content);
RouteData.DataTokens["umbraco"] = content;
}
public ActionResult Index()
{
//return view etc
}
但是我卻越來越
Exception Details: System.NullReferenceException:
Object reference not set to an instance of an object.
這裏:
Source Error(AddCouponCodesViewModel.cs):
Line 20:
Line 21: }
Line 22: public AddCouponCodesViewModel(IPublishedContent content)
Line 23: : base(content)
Line 24: {
AddCouponCodeRenderModel.cs:
public class AddCouponCodesViewModel : RenderModel
{
public string test { get; set; }
public List<string> tables { get; set; }
public List<string> errors { get; set; }
public AddCouponCodesViewModel(IPublishedContent content, CultureInfo culture) : base(content, culture)
{
}
public AddCouponCodesViewModel(IPublishedContent content)
: base(content)
{
}
這是Global.asax
public class Global : UmbracoApplication
{
protected override void OnApplicationStarted(object sender, EventArgs e)
{
base.OnApplicationStarted(sender, e);
BundleConfig.RegisterBundles(BundleTable.Bundles);
//AreaRegistration.RegisterAllAreas();
//WebApiConfig.Register(GlobalConfiguration.Configuration);
//FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
//RouteConfig.RegisterRoutes(RouteTable.Routes);
base.OnApplicationStarting(sender, e);
RouteTable.Routes.MapRoute(
"AddCouponCodes", // Route name
"Admin/{controller}/{action}/{id}", // URL with parameters
new { controller = "AddCouponCodes", action = "Index", id = "" } // Parameter defaults
);
}
}
內容發佈(我查和雙重檢查),節點ID是正確的。
我基本上試圖在這裏做的是獲取路線example.com/Admin/{controller}/{action}/{parameter} 路由,但有問題連接它與umbracoNode(和類RenderModel需要iPublishContent對象作爲參數,而是試圖通過它的任何東西)
可能有人請幫助我在這裏的時候,我在沒有運氣,一直停留太多的時間在這個:-(
您應該顯示「RenderModel」類的代碼。根據你說錯誤發生的地方,它看起來像是在該類的構造函數中發生錯誤。 –