MVC displaying image url from the results of a Uni Selector of custom page type

Matt Larrabee asked on June 19, 2020 22:19

Hi All. In Kentico 12sp Mvc, I have a custom page type Location and one of the fields is a Uni selector that allows adding multiple employees (which is also a custom page type). The Uni selector is working and I am able to display fields of the employees on my location view using the ValidationHelper like so: ValidationHelper.GetString(page.GetValue("Position"), "", "")

The issue is that the employee also has a photo. I'm not able to display the photo. When I use the following ValidationHelper.GetString(page.GetValue("Photo"), "", "") I get what looks to be a Guid. I've tried using the MediaFileInfoProvider and other methods to get the url of the photo to display but I'm not able to get it working.

Is there a way to get the image url of a file content field by Guid? Thanks!

Correct Answer

Dmitry Bastron answered on June 22, 2020 15:38

If you have DancingGoat MVC example site, the similar approach is used to display ArticleTeaser image. I highly recommend looking at this example as it has a few more very useful Html helpers.

In a nutshell, direct uploader stores these files as Attachments. Your generated class for Employee page type should contain a field of DocumentAttachment type for it and can be accessed like this: employee.Fields.Photo. You can add it to your view model:

employeeViewModel.Photo = employee.Fields.Photo;

And later in the view you can use it like this:

<img src="@Url.Kentico().ImageUrl(employeeViewModel.Photo.GetPath(), SizeConstraint.Empty)">
0 votesVote for this answer Unmark Correct answer

Recent Answers


Dmitry Bastron answered on June 22, 2020 10:56

Hi Matt,

Could you tell a bit more how the Photo field of Employee page type is configured (data type and control)? Is it using Text with Media selection control, File with Direct uploader or anything else?

0 votesVote for this answer Mark as a Correct answer

Matt Larrabee answered on June 22, 2020 14:48

Hi Dmitry, the Photo field is a File datatype with a Direct Uploader form control. Thanks

0 votesVote for this answer Mark as a Correct answer

Matt Larrabee answered on June 26, 2020 20:38

Thanks Dmitry, I think I was approaching this the wrong way. I was using ValidationHelper.GetString(page.GetValue("field")) to get the value of the tree node returned from the uniselector but I ended up getting it working using something similar to what you suggested. Example of what I ended up using. Uni selector field "Staff" returns NodeGuid of the custom Employee page type

string[] nodes = location.Staff.Split(';'); if(nodes.Length > 0) { for(var i=0;i<nodes.Length;i++) { var employee = EmployeeRepository.GetEmployee(Guid.Parse(nodes[i]));

    var emp = GetPageViewModel(new EmployeeViewModel()
    {
        Employee = employee
    }, employee.Name);

    ...
}

}

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.