Copied to clipboard

Flag this post as spam?

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


  • Emil Christiansen 66 posts 148 karma points
    Oct 22, 2015 @ 06:43
    Emil Christiansen
    0

    Problems with two views/templates related to the same documenttype

    Hi there!

    On an Umbraco version 7.1.6 i am having the following problem:

    • I have a documenttype called "Product"
    • I have a controller called "ProductController"
    • I have a model called "ProductViewModel" which inherits from the Umbraco "RenderModel"ยจ
    • I have a view/template called "Product"

    Everything works fine.

    Now i want to add an alternative view/template to the product.

    So i create a new view/template called: "NewProduct"

    Appart from the name, the "NewProduct" view is a 100% identical to the "Product" view.

    Now, when i change the view/template from "Product" to "NewProduct" on a product in the Umbraco backoffice - i get the following YSOD when i try to access the product:

    The model item passed into the dictionary is of type 'Umbraco.Web.Models.RenderModel', but this dictionary requires a model item of type 'Customer.MVC.Models.ViewModels.ProductViewModel'
    

    How can that be? The new view "NewProduct" is identical to the "Product" view - so it also inherits the "ProductViewModel" which inherits from the Umbraco "RenderModel" as it should...

    Am i trying to do something that cannot be done, or am i just doing something completely wrong?

    Thanks in advance!

  • John Churchley 272 posts 1258 karma points c-trib
    Oct 22, 2015 @ 08:32
    John Churchley
    0

    In your controller are you returning: return CurrentTemplate(model); or return View("Product", model);

    ?

  • Emil Christiansen 66 posts 148 karma points
    Oct 22, 2015 @ 08:33
    Emil Christiansen
    0

    I am returning: CurrentTemplate(model);

  • John Churchley 272 posts 1258 karma points c-trib
    Oct 22, 2015 @ 09:06
    John Churchley
    0

    Hi Emil,

    I think your returning the base model not your custom viewmodel in the controller, I just coincidently experiences the same YSOD as I was creating a test case/example! The example below should work and hopefully help you find the solution by comparison.

    John

    Controller

     public class ProductController : Umbraco.Web.Mvc.RenderMvcController
        {
            //
            // GET: /Product/
    
            public override ActionResult Index(RenderModel model)
            {
    
                ProductViewModel viewModel = new ProductViewModel(model.Content, model.CurrentCulture);
    
                viewModel.SKU = "UUU13113";
                //Do some stuff here, then return the base method
                return View(viewModel);
    
    
            }
        }
    

    ViewModel

        namespace Umbraco7.Models.ViewModels
    {
        public class ProductViewModel: RenderModel
        {
            public string SKU { get; set; }
    
            public ProductViewModel(IPublishedContent content, CultureInfo culture)
                : base(content, culture)
            {  }
    }
    
    }
    

    Views

    Product.cshtml

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<Umbraco7.Models.ViewModels.ProductViewModel>
    @{
        Layout = null;
    }
    
    <h1>Product</h1>
    @Model.SKU
    

    NewProducts.cshtml

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<Umbraco7.Models.ViewModels.ProductViewModel>
        @{
        Layout = null;
        }
        <h1>New Product</h1>
        @Model.SKU
    
  • Emil Christiansen 66 posts 148 karma points
    Oct 22, 2015 @ 09:09
    Emil Christiansen
    100

    Found the problem!

    In the controller - there was no "index" method... but a "product" method.

    That, of course, means that the method only was executed when the "product" template was chosen due to the routing rules.

    Check this:

    https://our.umbraco.org/documentation/reference/routing/custom-controllers

    Under the section "routing via template".

    Thanks for the feedback!

  • John Churchley 272 posts 1258 karma points c-trib
    Oct 22, 2015 @ 09:16
    John Churchley
    0

    Glad you found your solution! This code might help if you want to pass the sku in a query string.

    [NonAction]
            public override ActionResult Index(RenderModel model)
            {
                return View();
            }
    
            public ActionResult Index(RenderModel model, string sku)
            {
              ProductViewModel viewModel = new ProductViewModel(model.Content, model.CurrentCulture);
    
                viewModel.SKU = sku;
                //Do some stuff here, then return the base method
                return View(viewModel);
            }
    
Please Sign in or register to post replies

Write your reply to:

Draft