Get value definied in Umbraco CMS on C# method level
Hello everyone 👋
A few hours ago I started my adventure with Umbraco (and so far everything has been great) and I have come to the point where I need to use the declared value with Umbraco CMS on c# method level:
public class WeatherViewComponent : ViewComponent
{
public IViewComponentResult Invoke()
{
//get city name definied in Umbraco CMS
var city = ???;
var weather = getWeather(city);
return View("Default",weather);
}
}
In documentation, I read about IPublisdehContent and I try do something like this :
public class WeatherViewComponent : ViewComponent
{
private readonly IPublishedContent _publishedContent;
public TwitterViewComponent(IPublishedContent publishedContent)
{
_publishedContent = publishedContent;
}
public IViewComponentResult Invoke()
{
var city = _publishedContent.Value("city");
var weather = getWeather(city);
return View("Default",weather);
}
}
But in this case i got error Unable to resolve service for type 'Umbraco.Cms.Core.Models.PublishedContent.IPublishedContent'
I think you might have over complicated it slightly. Try doing this:
public class WeatherViewComponent : ViewComponent
{
[ViewComponent(Name = "Weather")]
public IViewComponentResult Invoke(IPublishedContent content)
{
var city = content.Value<string>("city");
var weather = getWeather(city);
return View("Default",weather);
}
}
Get value definied in Umbraco CMS on C# method level
Hello everyone 👋
A few hours ago I started my adventure with Umbraco (and so far everything has been great) and I have come to the point where I need to use the declared value with Umbraco CMS on c# method level:
In documentation, I read about IPublisdehContent and I try do something like this :
But in this case i got error Unable to resolve service for type 'Umbraco.Cms.Core.Models.PublishedContent.IPublishedContent'
Hi
I think you might have over complicated it slightly. Try doing this:
Kind regards
Paul
is working on a reply...