I have a list:
public List<MyModel> MyModels { get; set; }
How would I display one piece of information from one object within that list using Razor syntax?
I want to do something like:
@Html.Display(MyModel.Select(o => o.objectId = selectedObjectVal).FirstOrDefault().TextThatNeedsDisplayed)
But that does not compile. How would I properly display the information I want?
When I use the updated code above (retrieved from an answer below) I get the following error: CS1061: ‘int’ does not contain a definition for ‘grade’ and no extension method ‘grade’ accepting a first argument of type ‘int’ could be found (are you missing a using directive or an assembly reference?). The IntelliSense is giving me options of “Compare To” “Equals” “GetHashCode” etc. . .
Update:
If I do a .ToString()) at the end in place of “TextThatNeedsDisplayed” (and change from @Html.Display to @Html.Raw) it returns the data that it is searching by – literally what is stored at “o.objectId = selectedObjectVal” . I don’t know why this is – shouldn’t it be trying to turn the entire MyModel into a string? Still not showing TextThatNeedsDisplayed.
Assuming that your Model is a list of (View)Models you could access a property of one model in many ways.
if the list is the model for your razor view:
or just select one object from your list:
Hope I understood your question right.