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?
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)
{ }
}
}
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:
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:
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!
In your controller are you returning: return CurrentTemplate(model); or return View("Product", model);
?
I am returning: CurrentTemplate(model);
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
ViewModel
Views
Product.cshtml
NewProducts.cshtml
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!
Glad you found your solution! This code might help if you want to pass the sku in a query string.
is working on a reply...