Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • David Dupont 61 posts 115 karma points
    Nov 29, 2012 @ 20:32
    David Dupont
    0

    Pass strongly type model in razor view

    Hi everyone,

    I'm trying to implement strongly type model in controller using Umbraco 4.10 and I can't figure what I'm doing wrong.

    Here is my razor view:

    @model Umbraco.Web.Mvc.UmbracoViewPage<Emakina.Umbraco.DocumentTypes.TextPage>
    @{
        this.Layout = "SiteRoot.cshtml";
    } 

    And here is my custom controller :

        public class TextPageController : RenderMvcController
        {
            public override ActionResult Index(RenderModel model)
            {
                //we will create a custom model
                var customModel = ContentHelper.GetCurrentContent() as TextPage;
    
                //Example for 4.10:
    
                //get the template name from the route values:
                var template = ControllerContext.RouteData.Values["action"].ToString();
                //return an empty content result if the template doesn't physically 
                //exist on the file system
                if (!EnsurePhsyicalViewExists(template))
                {
                    return Content("");
                }
                //return the current template with an instance of MyCustomModel
                return View(template, customModel);
            }
        }
    

    When I call the page I get this ysod and I wonder why since the model returned in controller is TextPage and the model declared in razor view is TextPage.

    Thanks per advance.

    DDU

  • David Dupont 61 posts 115 karma points
    Nov 29, 2012 @ 20:57
    David Dupont
    0

    Here is the error :

    [InvalidOperationException: The model item passed into the dictionary is of type 'Emakina.Umbraco.DocumentTypes.TextPage', but this dictionary requires a model item of type 'Umbraco.Web.Mvc.UmbracoViewPage`1[Emakina.Umbraco.DocumentTypes.TextPage]'.]
       System.Web.Mvc.ViewDataDictionary`1.SetModel(Object value) +403
       System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary dictionary) +708
       System.Web.Mvc.WebViewPage`1.SetViewData(ViewDataDictionary viewData) +74
       System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +139
       System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +384
       System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +33
       System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +818804
       System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +265
       System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +818320
       System.Web.Mvc.Controller.ExecuteCore() +159
       System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +335
       System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +62
       System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +20
       System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +54
       System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +469
       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +375
  • David Dupont 61 posts 115 karma points
    Nov 29, 2012 @ 21:14
    David Dupont
    0

    Forget about it, the following line was the culprit :

    this.Layout="SiteRoot.cshtml";
  • Edwin van Koppen 156 posts 270 karma points
    Dec 04, 2012 @ 16:44
    Edwin van Koppen
    0

    You give the wrong object back, i stopped using the index to override and use my own controller like this

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using Umbraco.Web.Models;
    using Umbraco.Web.Mvc;
    using umbraco.cms.businesslogic.template;
    using ViewModel;

    namespace W3S.Controllers
    {
        public class HomeController : W3S.Models.Master
        {
            public ActionResult Home(RenderModel Model) {
                HomeViewModel HVM = new HomeViewModel();
                HVM.UmbracoModel = Model;

                return View(HVM);
            }
           

        }
       
    }
    namespace ViewModel {
        public class HomeViewModel {
            public RenderModel UmbracoModel { get; set; }
        }
    }

    @model ViewModel.HomeViewModel
    @{
        ViewBag.Title = "Test";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }

    <h2>@Model.UmbracoModel.Content.GetProperty("titel").Value</h2>
    <div>@Html.Raw((string)Model.UmbracoModel.Content.GetProperty("body").Value)</div>

     

     

  • Edwin van Koppen 156 posts 270 karma points
    Dec 05, 2012 @ 08:51
    Edwin van Koppen
    0

    I'm busy with the same problem but i stumbled upon this one http://our.umbraco.org/forum/core/general/36526-U411-Looks-like-NodeUrl-functionality-is-broken?p=0

    So strong typed objects you cannot get a url... pretty big problem for me.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies