Can't retrieve property value within a partial view
For this specific project, I'm working with partial views to create different panels for the home page in a website. Using Umbraco's backoffice, I created a document type for the home page, and using the composition feature, now have one document property - a text box (which will be used to pass in a link from the user to the website) - am starting slow here. There is a corresponding template for the homepage as well.
Now I'm in VS, and am trying to get content from that property value in the _sidepanelpoll.cshtml file (that is rendered in the home.cshtml file via @{ Html.RenderAction("RenderSidePanelPoll", "Home");} But the @Model.Value("link") - 'link' is the alias for the property in the Home document type - isn't working.
I'm trying to use @inherits Umbraco.Web.Mvc.UmbracoViewPage AND @using ContentModels = Umbraco.Web.PublishedModels; but still throw an System.NullReferenceException exception and my VS is showing a squiggly red line under .Home of the inherits statement - I suspect I'm not using the right model name (or should I avoid using a strongly typed model here given it's a partial view?).
I want to use @Umbraco.AssignedContentItem.GetPropertyValue('link') along with @inherits UmbracoViewPage at the top but that's no longer available in Umbraco 8. What are my options? Thank you!
Edited to add - I noticed my App_Data folder is grayed out; when I right-click and select, 'Include in Project', I get all kinds of duplicate errors (e.g., The type 'Home' already contains a definition for 'Model TypeAlias', etc) for the models.generated.cs file. Not sure if this is related in any way.
you have to pass the model to the partial view in order to access the properties. You can achieve this by rendering the partial like this
@Html.Partial("_sidepanelpoll", Model)
or, if you have to call the controller action for some reason, you can add a parameter to your action and pass it like this:
@{ Html.RenderAction("RenderSidePanelPoll", "Home", new { model = Model });}
Then, in your Controlleraction, you can pass the model to the view like this:
return PartialView("_sidepanelpoll", model);
Regarding the duplicate errors, I need some more information. Please provide the code where the error occurs. I assume that these errors come from the modelsbuilder.
Thanks Jonathan, you're right - passing the model to the partial view did the trick (why didn't I think of this, I don't know. Oh Umbraco ...).
As for the App_Data folder, when I include it in the project and do a build, I get all kinds of 'duplicates! you've got duplicates!' errors for the models.generated.cs file. I've included a snapshot for your review:
Can't retrieve property value within a partial view
For this specific project, I'm working with partial views to create different panels for the home page in a website. Using Umbraco's backoffice, I created a document type for the home page, and using the composition feature, now have one document property - a text box (which will be used to pass in a link from the user to the website) - am starting slow here. There is a corresponding template for the homepage as well.
Now I'm in VS, and am trying to get content from that property value in the _sidepanelpoll.cshtml file (that is rendered in the home.cshtml file via @{ Html.RenderAction("RenderSidePanelPoll", "Home");} But the @Model.Value("link") - 'link' is the alias for the property in the Home document type - isn't working.
I'm trying to use @inherits Umbraco.Web.Mvc.UmbracoViewPage AND @using ContentModels = Umbraco.Web.PublishedModels; but still throw an System.NullReferenceException exception and my VS is showing a squiggly red line under .Home of the inherits statement - I suspect I'm not using the right model name (or should I avoid using a strongly typed model here given it's a partial view?).
I want to use @Umbraco.AssignedContentItem.GetPropertyValue('link') along with @inherits UmbracoViewPage at the top but that's no longer available in Umbraco 8. What are my options? Thank you!
Edited to add - I noticed my App_Data folder is grayed out; when I right-click and select, 'Include in Project', I get all kinds of duplicate errors (e.g., The type 'Home' already contains a definition for 'Model TypeAlias', etc) for the models.generated.cs file. Not sure if this is related in any way.
Hi,
you have to pass the model to the partial view in order to access the properties. You can achieve this by rendering the partial like this
or, if you have to call the controller action for some reason, you can add a parameter to your action and pass it like this:
Then, in your Controlleraction, you can pass the model to the view like this:
Regarding the duplicate errors, I need some more information. Please provide the code where the error occurs. I assume that these errors come from the modelsbuilder.
Jonathan
Thanks Jonathan, you're right - passing the model to the partial view did the trick (why didn't I think of this, I don't know. Oh Umbraco ...).
As for the App_Data folder, when I include it in the project and do a build, I get all kinds of 'duplicates! you've got duplicates!' errors for the models.generated.cs file. I've included a snapshot for your review:
Hi Nancy,
I'm glad it helped!
looks like your problem is caused by the modelsbuilder. Take a look at this thread: https://our.umbraco.com/forum/using-umbraco-and-getting-started/78340-error-cs0579-duplicate-publishedcontentmodel-attribute
Probably you are currently not using the modelsbuilder, so I see two possible solutions:
For more information about the ModelsBuilder, take a look at this Doc: https://our.umbraco.com/documentation/Reference/Templating/Modelsbuilder/
Jonathan
You are awesome. Thank you, will be reviewing these links.
is working on a reply...