asp.net mvc 3 razor get one element from IEnumerable

后端 未结 3 916
Happy的楠姐
Happy的楠姐 2021-02-19 13:17

I my view model(LIST) looks like this:

public class ConversationModel 
    {
        public int ID { get; set; }
        public string Body { get; set; }
                


        
3条回答
  •  自闭症患者
    2021-02-19 13:44

    You should be able to do:

    @Model.First().ToUserID
    

    Note, you may want to check whether there are any items in the enumerable first as if there aren't, then First() will return null

    (Note, because you had @Model[0] in your question, I assume you are specifically trying to get the first value. I may have the wrong end of the stick, in which case Jakub's answer should sort you out!)

提交回复
热议问题