jacksorjacksor (Richard Jackson)
10/28/2023, 8:09 PMModel.Value<IHtmlString>("productDescription"))
rather than Model.productDescription
(second image) which isn't ideal.
Could anyone advise me on this? I've included HomeViewModel.cs
, HomeController.cs
and home.cshtml
for transparency of my current approach. Greatly appreciate any input.D_Inventor
10/29/2023, 6:06 AMjacksorjacksor (Richard Jackson)
10/29/2023, 9:02 AMHome
fields in the HomeController
I've been casting the CurrentPage
as Home
- is that correct?
2) CurrentPage
can be nullable - what would be the best way to handle this? I'm assuming it'd be like if(CurrentPage == null){return ErrorPage]
, I'm just not too sure on how to call an error page as an IActionResult (if that's correct!)
Code of HomeController.cs
csharp
public override IActionResult Index()
{
var currentPage = CurrentPage as Home;
var homeViewModel = new HomeViewModel(CurrentPage,
new PublishedValueFallback(_serviceContext, _variationContextAccessor))
{
HasImage = !currentPage.MainImage.IsObjectNullOrEmptyString()
};
return CurrentTemplate(homeViewModel);
}
D_Inventor
10/29/2023, 9:10 AMCurrentPage
can be null, I don't think it ever is in practice. I usually go with a simple pattern match like this:
if (CurrentPage is not Home content) return CurrentTemplate(CurrentPage)
// do the rest
I usually just return the current template, but it's also a good place to throw an InvalidOperationException
, whichever has your preference.jacksorjacksor (Richard Jackson)
10/29/2023, 9:11 AM