asp.net mvc 3 razor get one element from IEnumerable

后端 未结 3 893
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:57

    You should be able to use the following:

    @Model.First().ToUserID
    

    However, if your model will only ever reference the first element of the enumeration in the view, I would recommend that you only pass that element to the view.

    For example:

    @model ConversationModel
    
    @Model.ToUserID
    

    And in the controller only pass the first element that is required:

    List conversationList = //your conversation model initialisation code
    return View(conversationList.First());
    

提交回复
热议问题