public class ViewProfileController : RenderMvcController
{
public ViewProfileController() : this(UmbracoContext.Current) { }
public ViewProfileController(UmbracoContext umbracoContext) : base(umbracoContext) { }
public override ActionResult Index(RenderModel model)
{
string profileURLtoCheck = Request.RequestContext.RouteData.Values["profileURLtoCheck"].ToString();
ViewProfileViewModel profile = new ViewProfileViewModel();
if (!String.IsNullOrEmpty(profileURLtoCheck))
{
var ms = UmbracoContext.Current.Application.Services.MemberService;
var findMember = ms.GetAllMembers().FirstOrDefault(x => x.GetValue<string>("profileURL") == profileURLtoCheck);
if (findMember != null)
{
profile.Name = findMember.Name;
}
}
return CurrentTemplate(profile);
}
}
And the model is this:
public class ViewProfileViewModel : RenderModel
{
public ViewProfileViewModel() : base(UmbracoContext.Current.PublishedContentRequest.PublishedContent) { }
public string Name { get; set; }
}
When I try to navigate to the route e.g. http://www.mysite.co.uk/user/joebloggs I keep getting errors. If I inherit from RenderModel in my model as shown above I get:
Object reference not set to an instance of an object.
on the constructor, and if I don't inherit from RenderModel I get the following error:
Cannot bind source type ViewProfileViewModel to model type Umbraco.Web.Models.RenderModel
I seem to be going around in circles .... any help would be greatly appreciated.
What happens if you add an IPublishedContent parameter to your constructor, like this:
public class ViewProfileViewModel : RenderModel
{
public ViewProfileViewModel(IPublishedContent content) : base(content) { }
public string Name { get; set; }
}
If I add the line of code to the Model as you suggested, I get an error in the Controller on this line:
ViewProfileViewModel profile = new ViewProfileViewModel();
There is no argument given that corresponds to the required formal parameter 'content' of 'ViewProfileViewModel.ViewProfileViewModel(IPublishedContent)
If I update the controller line to:
ViewProfileViewModel profile = new ViewProfileViewModel(CurrentPage);
I get the following error:
DataTokens must contain an 'umbraco-doc-request' key with a PublishedContentRequest object
I assume this is because /user/profilename isn't an Umbraco page?
Custom Route, Controller and Model issue
Hello :-)
I'm trying to implement a "View Profile" page using a custom route, controller and model. But can't get it to work :-(
My custom route is:
My controller is:
And the model is this:
This is my View:
When I try to navigate to the route e.g. http://www.mysite.co.uk/user/joebloggs I keep getting errors. If I inherit from RenderModel in my model as shown above I get:
Object reference not set to an instance of an object.
on the constructor, and if I don't inherit from RenderModel I get the following error:
Cannot bind source type ViewProfileViewModel to model type Umbraco.Web.Models.RenderModel
I seem to be going around in circles .... any help would be greatly appreciated.
What happens if you add an IPublishedContent parameter to your constructor, like this:
Hi Micha,
If I add the line of code to the Model as you suggested, I get an error in the Controller on this line:
There is no argument given that corresponds to the required formal parameter 'content' of 'ViewProfileViewModel.ViewProfileViewModel(IPublishedContent)
If I update the controller line to:
I get the following error:
DataTokens must contain an 'umbraco-doc-request' key with a PublishedContentRequest object
I assume this is because /user/profilename isn't an Umbraco page?
For creating an instance of your ViewProfileViewModel, I think you need to use model.Content instead of CurrentPage:
Still get an error, on that line :-( ...
Isn't this because model.Content is null because my custom route / view isn't actually an Umbraco page?
So if I understand you correctly, there is no node available to refer to.
For this approach you need an instance of IPublishedContent, so without a node being available, a virtual node must be created/found.
The following resources can help you further on that topic:
http://shazwazza.com/post/custom-mvc-routes-within-the-umbraco-pipeline/
https://stackoverflow.com/questions/35647220/how-to-create-a-virtual-node-in-umbraco
is working on a reply...