System.NullReferenceException创建viewModel

时间:2013-09-24 18:26:55

标签: c# asp.net-mvc asp.net-mvc-routing umbraco

所以,我正在尝试找到Umbraco节点(作为iPublishedContent),并将其传递给viewModel(因为Ш已经劫持了一条路线)。所以我把它放在我的控制器中:

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对象作为参数,但是在尝试传递任何东西时我都没有运气)

有人可以帮助我,在这上面被困了太多时间: - (

3 个答案:

答案 0 :(得分:1)

澄清一下,如果你正在劫持一条路线,那就意味着你要覆盖Umbraco将RenderModel传递给其中一个已发布网页的方式。您可以通过覆盖主RenderMvcController来全局执行此操作,也可以在DocumentType特定的基础上覆盖。例如,如果我有一个主页doc类型,我可以创建:

public HomepageController : RenderMvcController
{
    public override ActionResult Index(RenderModel model)
    {
        // Create your new renderModel here, inheriting
        // from RenderModel

        return CurrentTemplate(renderModel);
    }
}

这将通过这一个动作将所有呼叫路由到主页。为此,您需要在路由表中定义任何新路由。您应该在动作中覆盖渲染模型,而不是在构造函数中。

你的问题有点令人困惑,而且你想要实现的目标并不完全清楚,因为:

  • 您已定义路线,
  • 在构造函数中,您调用Umbraco.TypedContent(1225)来检索特定的已发布节点

所以...如果您尝试路由的管理页面本身已由Umbraco发布(并且听起来不像它),则只需创建一个新控制器,其中包含页面文档类型和覆盖的名称以上述方式呈现模型。

但是......如果您的管理页面尚未由Umbraco发布,而您只想让管理页面访问节点数据,那么您有几个选择:

  • 创建一个表面控制器,继承自SurfaceController。这将使您可以访问Umbraco上下文等;或
  • 创建标准控制器(最好在区域中)并使用Autofac
  • 之类的东西注入ContentCache

E.g:

builder.RegisterControllers(typeof (AdminController).Assembly)
       .WithParameter("contentCache", UmbracoContext.Current.ContentCache);
  • 创建标准控制器(最好在区域中)并使用Umbraco的ContentService API访问节点,即new Umbraco.Core.Services.ContentService().GetById(1225)

最后两种方法之间的区别在于:

  • 注入ContentCache可让您以非常快速的方式访问已发布的内容。
  • 访问ContentService可以为您提供对节点本身的读/写访问权限,但在您直接查询数据库时会以速度为代价。

这取决于您的要求。

无论哪种方式,都值得花时间阅读documentation for hijacking Umbraco routes,至少试图了解正在发生的事情。

答案 1 :(得分:0)

好吧,我可以告诉你,你的观点没有为Razor标记提供任何东西,因为你的Index方法不能提供任何东西。这是一个问题。我还可以告诉你,在你的AddCouponCodesViewModel中,你需要一个空的构造函数,这样剃刀语法就可以创建一个实例,然后填充它以使你提交的对象与视图相匹配。

修改ViewController:

  public ActionResult Index()
  {
        return View(viewModel);
  }

修改AddCouponCodesViewModel以添加Empty构造函数:

public AddCouponCodesViewModel()
{
}

答案 2 :(得分:0)

在您的视图模型上创建一个无参数的构造函数,如下所示:

 public AddCouponCodesViewModel(): 
    this(new UmbracoHelper(UmbracoContext.Current).
TypedContent(UmbracoContext.Current.PageId))
   {
   }

这将获得您的其他构造函数正在寻找的上下文。 在使用特定构造函数创建类之后,编译器默认会停止生成无参数构造函数。既然你需要一个无参数的构造函数,那就是如何得到一个并且仍然传递你的viewmodel需要的Umbraco上下文信息

相关问题