-3

I am not sure how to even title or ask this question, so apologies for any confusion.

I need to get the value of the Id from the first list in Model.Content.GetPropertyValue("SlidePanel"). I've tried many, many, many thing with my troubles usually being that "You can't do this because it's an Object". In the image below, I want to get the Id Value of "1092" as a String.

-----EDIT-------

I was able to get the value with the code below. Casted the list, grabbed the first list since it would always be that option (I wrapped it in an if, but removed it in this example), then I was able to specify the property I needed and converted as needed.

If I sound like I don't speak this language fluently, it's because I'm still fresh to development. Thanks for everyone who helped.

 dynamic slidePanelObject = Model.Content.GetProperty("SlidePanel").Value;
 List<object> slidePanelCast = ((IEnumerable<object>)slidePanelObject).Cast<object>().ToList();
 dynamic slidePanelFirst = slidePanelCast.First();
 var slidePanelId = slidePanelFirst.Id;
 string slidePanelString = slidePanelId.ToString();
Austin V.
  • 79
  • 7

1 Answers1

2

Model.Content.GetPropertyValue is probably returning an System.Object, so you'll need to cast your temp var to a List<T> type of some kind before you can access it like a list.

Without knowing all the types involved, here's some code that you could modify:

var temp = Model.Content.GetPropertyValue("SlidePanel") as List<TYourType>;
Victor Wilson
  • 1,720
  • 1
  • 11
  • 22
  • var temp = Model.Content.GetPropertyValue("SlidePanel") as List; Tried this but I get null. I tried to figure out why, but I was not able to, so sorry about needing my hand held. – Austin V. Mar 09 '20 at 18:55
  • That's expected behavior for the `as` operator if you attempt to cast to the wrong type. We need more information to know what kind of collection you need to cast to. What kind of property is `SlidePanel`? – Victor Wilson Mar 09 '20 at 18:59
  • I'm not sure if this is the answer that you are looking for, but SlidePanel is a collection. I would assume the specific value I want (Id) would either be an int or a string, but I am not sure how I would confirm. – Austin V. Mar 09 '20 at 19:38
  • @AustinV. Can you update your post with the text of the whole class? – Victor Wilson Mar 09 '20 at 21:35
  • 1
    Casting it to a list was the way to go. Thanks for the help! – Austin V. Mar 10 '20 at 19:47