Here is my problem, how can I get the image url for each product?
System.Web.Http.HttpGet]
public List<KeyValuePair<int, string>> ComparisonKeyValues(string ids)
{
if (!string.IsNullOrEmpty(ids))
{
var nodes = ids.Split(',').ToList().TypedContentList();
return nodes.Select(x => new KeyValuePair<int, string>(x.Id, x.GetPropertyValue<string>("title"))).ToList();
// I want to get also the x.GetPropertyValue<string>(productImage).Url
}
return new List<KeyValuePair<int, string>>();
}
Depends... what is productImage ? A property, an object (don't think this code will compile, don't see productImage defined somewhere?
If it's a property, which I'm assuming ,because you're using x.GetPropertyValue(), then it will still depend on how productImage property was defined? Either as upload or a media picker?
If the first, then
x.GetPropertyValue<string>("productImage")
will already return path to the image, otherwise, you'd need to
Yes it's a property, but when I add x.GetPropertyValue<string>("productImage") it says KeyValuePair does not contain a constructor that takes 3 arguments.
return nodes.Select(x => new KeyValuePair<int, string>(x.Id, x.GetPropertyValue<string>("title"), x.GetPropertyValue<string>("productImage")).ToList();
if you want to use the current format of KeyValuePair<>, you'd have to extend the second generic type to accept multiple values, so would be something along the lines of
[System.Web.Http.HttpGet]
public List<Compares> CompareValues(string ids)
{
var result = new List<Compares>();
if (!string.IsNullOrEmpty(ids))
{
var nodes = ids.Split(',').ToList().TypedContentList();
return nodes.Select(x => new KeyValuePair<int, string>(x.Id, x.GetPropertyValue<string>("title"))).ToList();
/// Error : Cannot implicity convert type 'System.Collections.Generic.List....
// what should I put in here?
}
return result;
}
public List<Compares> CompareValues(string ids)
{
var result = new List<Compares>();
if (!string.IsNullOrEmpty(ids))
{
string[] listOdIds = ids.Split(new char[] { ',' });
foreach (var id in listOdIds)
{
var page = Umbraco.TypedContent(id);
var imageUrl = Umbraco.TypedMedia(page.GetPropertyValue<string>("image")).Url;
var comparess = new Compares()
{
Id = Convert.ToInt32(id),
Title = page.GetPropertyValue<string>("title"),
Url = imageUrl
};
result.Add(comparess);
}
return result;
}
return result;
}
How to get 3 values using KeyValuePair
Hi All,
Sorry for noob question.
Here is my problem, how can I get the
image urlfor each product?Any help for this?
Thanks in advance.
I am new in C#, and also in Umbraco.
Jin
Hi,
Depends... what is productImage ? A property, an object (don't think this code will compile, don't see productImage defined somewhere?
If it's a property, which I'm assuming ,because you're using x.GetPropertyValue(), then it will still depend on how productImage property was defined? Either as upload or a media picker?
If the first, then
will already return path to the image, otherwise, you'd need to
which returns a IPublishedContent, from where you can do .Url to get the path to the image
Hi Dirk,
Yes it's a property, but when I add
x.GetPropertyValue<string>("productImage")it saysKeyValuePair does not contain a constructor that takes 3 arguments.I want to return
ID, title, and Url.Thanks for your response.
Jin
Or you have any idea on how I can get all the properties using their
idsI want to get also the
productImageproperty.Any help for this, I'm stuck on this problem.
Thanks in advance,
Jin
Hi,
if you want to use the current format of KeyValuePair<>, you'd have to extend the second generic type to accept multiple values, so would be something along the lines of
which I believe is a bit overkill, why not create a specific class holding three properties and return as
Example:
and return a
from your method
Hi Dirk
What do you mean by overkill??
Like this?
Thanks dude.
Marky
Overkill = list of keyvaluepair where value is again a dictionary, bit too complex for what you'd need (and doesn't make it easier to read)
Okay..
Is still got an error
Thanks,
Marky
Because nodes.Select returns IEnumerable
Oh,
How can I rid of this? Any alternative way?
Sorry for my noob question, I really new in C# & Umbraco :(
Marky
Hi Dirk
I've got the correct code, thanks for your help.
Here what I've done.
Thanks,
Marky
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.