Try our conversational search powered by Generative AI!

problem with layout

Dov
Dov
Vote:
 

Hi, I start building a site with MVC and epiServer cms, creating layout  and homepage according to epiServer documantation and everything worked fine.

Now i want to add a page, which is not a contentType page (i dont extract data from the cms) but still use the layout i created.

I changed  the route by attribute and managed to get  to the controller, passing my own model.

The problem is  i got an error: the view expect to get other model. 

What should i do?

Thank you,

dovrat

#208225
Oct 17, 2019 20:56
Vote:
 

At the top of your view file, you have something like:

@model MyViewModel

<div class="border">
    <h2>@Model.PageName</h2>
    <p>@Model.TeaserText</p>
    <p>
          ...
    </p>
</div>

Do you pass in a model that matches the @model directive?

#208227
Edited, Oct 17, 2019 21:04
Dov
Vote:
 

hi, yes, i pass the correct model, and when i debug the view i even get the data correctly, but then i have an error that the view expecting other model

#208231
Oct 17, 2019 22:35
Dov
Vote:
 

but if i write in the view "@layout = null", then i see the view and everything is ok - but i dont have the layout, which i need

#208232
Oct 17, 2019 22:49
Vote:
 

Generally, the layout page is based on a model e.g. in Alloy layout is based on @model IPageViewModel<SitePageData> and this model has many properties depending on Page Data. Not sure using the same layout page could be the best approach.

Creating anew layout page for your views (views for those pages, not EPi Pages) could be an idea where you could define some strategy to select correct layout page or defining a layout in your view could also work. e.g.

rather 

@layout = null

use something like

@{
    Layout = "~/Views/Shared/NoIContent.cshtml";
}
#208238
Oct 18, 2019 9:17
Vote:
 

If you want to show both Episerver and non episerver content, it might be better to create two layout pages.

In _viewstart.chstml you can have something like this:

@{
    Layout = ViewContext.ViewBag.LayoutPage;
}

And then in PageContextActionFilter.OnResultExecuting you can set the layout like this:

var viewModel = filterContext.Controller.ViewData.Model;

// epi
if (viewModel is IPageViewModel<...>)
{
    filterContext.Controller.ViewBag.LayoutPage = "~/Somepath/Layout.cshtml";
}
else
{
...
}
#208256
Oct 18, 2019 12:57
Dov
Vote:
 

thank you, its solved my peoblem

#208298
Oct 20, 2019 15:54
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.