how to pass around custom "Model" along with the model from UmbracoViewPage/TemplatePage
Hi,
Thanks for all the tips and information.
This time I'm creating a tiny webpage. There we have a list of news articles, and if you click on one, it leads you to its content.
In doing so, because the list is near the buttom, I'm thinking of using ajax for pagination. Otherwise it would mean reloading the whole page everytime the user clicks on the next/prev button, which is very user unfriendly.
Thanks to the code shared by Mr.Paul, and I think I've got the basic flow.
The problem is, I'm getting an error saying the custom made Model ( by Mr.Paul actually, I basically copied and pasted it...) cannot be converted to the type of "IPublishedContent"
Cannot bind source type Umbraco.Web.Models.RenderModel to model type CodeShare.Library.Models.SearchResultsModel.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
So somehow I need to get "model", which is of type IPublishedContent and is used to render the content of newsposts and the list of newsposts, and custom made Model, which is used to store the listSize, what page the user etc, to coexist.
It sounds like you're not passing the correct model through to the SearchResults.cshtml partial view from the controller.
Based on Paul's code, it looks like it converts the submitted form data to a SearchResultsModel within the helper - can you confirm the model passed through is of the correct type?
Hi,
thanks for checking. Sorry if I mis-interpreted your answer, but I think I at least managed to get the SearchResults model in what in Paul's code is SearchResults.cshtml as I have the following.
Cannot bind source type Umbraco.Web.Models.RenderModel to model type CodeShare.Library.Models.SearchResultsModel.
The following is my version of SearchResults.cshtml. (named _NewsEntry.cshtml ).
As the name is different, it would be quite understandable if I had errors like "can't find this controller, can't find this partial view" but I'm getting errors in referencing (i.e. @inherits, @using, that kind of things)
Below is my razor, there's some Japanese texts here and there but they are not important syntactically so just ignore them:
@model CodeShare.Library.Models.SearchViewModel
@using CodeShare.Web.Controllers
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
<div class="carousel-inner">
<div class="carousel-item active">
<!--this part uses UmbracoTemplatePage to render the content dynamically-->
@{
var pageSize = 5;
IEnumerable<IPublishedContent> newsPosts = Umbraco.AssignedContentItem.DescendantOrSelf("news").Children.Where(x => x.IsVisible() && x.DocumentTypeAlias == "newsPost").OrderByDescending(x => x.UpdateDate);
var page = 1; int.TryParse(Request.QueryString["p"], out page);
var totalPages = (int)Math.Ceiling((double)newsPosts.Count() / (double)pageSize);
if (page > totalPages)
{
page = totalPages;
}
else if (page < 1)
{
page = 1;
}
}
@foreach (var item in newsPosts.Skip((page - 1) * pageSize).Take(pageSize))
{
var imagid = item.GetPropertyValue("image");
string imgurl = Umbraco.Media(imagid.ToString()).Url;
<a href="@item.Url" class="media">
<img src="@imgurl">
<div class="media-body">
<h5 class="mt-0">@item.Name</h5>
<span>最終更新日: @item.UpdateDate.ToString("yyyy/MM/dd") </span>
</div>
</a>
}
</div>
<!--from here I want to use the custom model, because the input will made by the user not in the backoffice-->
<!--in my understanding, with this beginform method below,
it sends those HiddenFor's as a set of parameters to the method called "SubmitSearchForm" in a controller class called "SurfaceController" -->
@using (Ajax.BeginForm("SubmitSearchForm", "Surface", new AjaxOptions()
{
UpdateTargetId = "carousel",
HttpMethod = "POST",
InsertionMode = InsertionMode.Replace,
}))
{
@Html.HiddenFor(m => m.DocTypeAliases)
@Html.HiddenFor(m => m.FieldPropertyAliases)
@Html.HiddenFor(m => m.PageSize)
@Html.HiddenFor(m => m.PagingGroupSize)
@Html.TextBoxFor(m => m.SearchTerm)
}
<button id="submit-button">Search</button>
<div class="carousel">
@{ Html.RenderAction("RenderSearchResults", "Surface", new { Model = Model.SearchResults });}
</div>
}
and the error is as follows:
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Parser Error Message: The 'inherits' keyword is not allowed when a 'model' keyword is used.
Source Error:
Line 1: @model CodeShare.Library.Models.SearchViewModel
Line 2: @using CodeShare.Web.Controllers
Line 3: @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
Line 4:
Line 5: <div class="carousel-inner">
Source File: /Views/Partials/Home/_NewsEntry.cshtml Line: 3
So in my understanding, I don't understand how to pass down the model stipulated by UmbracoTemplatePage or UmbracoViewPage, which both have the interface of IPublishedContent, along with the custom one.
If you happened to know good posts on this, I'd appreciate it if you could give me the link.
You don't need to use every single model in one view. You can easily isolate parts of your code in partial views (there are other ways but I'm trying not to make it complicated since it seems you're new) and pass then the partial views to the main view with their own models.
how to pass around custom "Model" along with the model from UmbracoViewPage/TemplatePage
Hi,
Thanks for all the tips and information.
This time I'm creating a tiny webpage. There we have a list of news articles, and if you click on one, it leads you to its content.
In doing so, because the list is near the buttom, I'm thinking of using ajax for pagination. Otherwise it would mean reloading the whole page everytime the user clicks on the next/prev button, which is very user unfriendly.
Thanks to the code shared by Mr.Paul, and I think I've got the basic flow.
The problem is, I'm getting an error saying the custom made Model ( by Mr.Paul actually, I basically copied and pasted it...) cannot be converted to the type of "IPublishedContent"
So somehow I need to get "model", which is of type IPublishedContent and is used to render the content of newsposts and the list of newsposts, and custom made Model, which is used to store the listSize, what page the user etc, to coexist.
How should I do this??
It sounds like you're not passing the correct model through to the SearchResults.cshtml partial view from the controller.
Based on Paul's code, it looks like it converts the submitted form data to a SearchResultsModel within the helper - can you confirm the model passed through is of the correct type?
Hi, thanks for checking. Sorry if I mis-interpreted your answer, but I think I at least managed to get the SearchResults model in what in Paul's code is SearchResults.cshtml as I have the following.
The following is my version of SearchResults.cshtml. (named _NewsEntry.cshtml ).
As the name is different, it would be quite understandable if I had errors like "can't find this controller, can't find this partial view" but I'm getting errors in referencing (i.e. @inherits, @using, that kind of things)
Below is my razor, there's some Japanese texts here and there but they are not important syntactically so just ignore them:
and the error is as follows:
So in my understanding, I don't understand how to pass down the model stipulated by UmbracoTemplatePage or UmbracoViewPage, which both have the interface of IPublishedContent, along with the custom one.
If you happened to know good posts on this, I'd appreciate it if you could give me the link.
Many thanks,
Hello tofu did you find any solution for this problem i am also facing same issue please help
error, Cannot bind source type Umbraco.Web.Models.ContentModel to model type Umbraco.Web.PublishedModels.BigDto.
You don't need to use every single model in one view. You can easily isolate parts of your code in partial views (there are other ways but I'm trying not to make it complicated since it seems you're new) and pass then the partial views to the main view with their own models.
So, your main view could be
And you could have a partial with
My syntax is probably wrong right now, but I think it will work for you
Hi @Hary,
How do you declare "itsmodel"? Say I want to use a model called Test that's under /Models folder, how do I pass this using correct syntax?
Did u find a solution to this problem, i am facing the same issue
is working on a reply...