Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
I know how to get current content in Umbraco 8 e.g.
https://our.umbraco.com/forum/umbraco-8/97200-get-current-ipublishedcontent
But how do I get the same context in Umbraco 9?
There is no Current.xxx
I need this in order to handle my custom view models.
namespace UmbracoProject.Core.ViewModels { public class LoginViewModel : BaseViewModel { // CAN'T Pass null public LoginViewModel() : base(Current.UmbracoContext.PublishedRequest.PublishedContent) { } public LoginViewModel(IPublishedContent content) : base(content) { } [Display(ResourceType = typeof(TextResource), Name = "Label_EmailAddress")] [Required(ErrorMessageResourceType = typeof(TextResource), ErrorMessageResourceName = "Error_Required")] [EmailAddress(ErrorMessageResourceType = typeof(TextResource), ErrorMessageResourceName = "Error_EmailAddress")] [DataType(DataType.EmailAddress)] public string EmailAddress { get; set; } [Display(ResourceType = typeof(TextResource), Name = "Label_Password")] [Required(ErrorMessageResourceType = typeof(TextResource), ErrorMessageResourceName = "Error_Required")] [DataType(DataType.Password)] public string Password { get; set; } } }
Hi Carl 👋
Current is not really a thing anymore in Umbraco V9 and you should use DI to get what you need in the constructor etc...
As you seem to be doing custom view models I suggest you take a look at this piece of documentation.
https://our.umbraco.com/documentation.../Reference/routing/custom-controllers#returning-a-view-with-a-custom-model
So it would be best to use PublishedContentWrapped
PublishedContentWrapped
public class LoginViewModel : PublishedContentWrapped { // The PublishedContentWrapped accepts an IPublishedContent item as a constructor public LoginViewModel(IPublishedContent content, IPublishedValueFallback publishedValueFallback) : base(content, publishedValueFallback) { } [Display(ResourceType = typeof(TextResource), Name = "Label_EmailAddress")] [Required(ErrorMessageResourceType = typeof(TextResource), ErrorMessageResourceName = "Error_Required")] [EmailAddress(ErrorMessageResourceType = typeof(TextResource), ErrorMessageResourceName = "Error_EmailAddress")] [DataType(DataType.EmailAddress)] public string EmailAddress { get; set; } [Display(ResourceType = typeof(TextResource), Name = "Label_Password")] [Required(ErrorMessageResourceType = typeof(TextResource), ErrorMessageResourceName = "Error_Required")] [DataType(DataType.Password)] public string Password { get; set; } }
Or alternatively as the documentation extend the partial classes that ModelsBuilder has generated.
I hope this helps you.
Warren 😀
Hi.
But the constructor must be empty for it to work with serialization and jquery-validate etc.
I managed to solve it by inheriting:
public class BaseViewModel : IContentModel
And setting my own Content.
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
Get current IPublishedContent for Umbraco 9
I know how to get current content in Umbraco 8 e.g.
https://our.umbraco.com/forum/umbraco-8/97200-get-current-ipublishedcontent
But how do I get the same context in Umbraco 9?
There is no Current.xxx
I need this in order to handle my custom view models.
Hi Carl 👋
Current is not really a thing anymore in Umbraco V9 and you should use DI to get what you need in the constructor etc...
As you seem to be doing custom view models I suggest you take a look at this piece of documentation.
https://our.umbraco.com/documentation.../Reference/routing/custom-controllers#returning-a-view-with-a-custom-model
So it would be best to use
PublishedContentWrapped
Or alternatively as the documentation extend the partial classes that ModelsBuilder has generated.
I hope this helps you.
Warren 😀
Hi.
But the constructor must be empty for it to work with serialization and jquery-validate etc.
I managed to solve it by inheriting:
And setting my own Content.
is working on a reply...