asp.net mvc 3剃须刀从IEnumerable获取一个元素 [英] asp.net mvc 3 razor get one element from IEnumerable
本文介绍了asp.net mvc 3剃须刀从IEnumerable获取一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的视图模型(LIST)如下:
I my view model(LIST) looks like this:
public class ConversationModel
{
public int ID { get; set; }
public string Body { get; set; }
public DateTime Datetime { get; set; }
public string Username { get; set; }
public string ImgUrl { get; set; }
public string ToUserID{ get; set; }
}
这是我的观点
@model IEnumerable<NGGmvc.Models.ConversationModel>
如何在当前位置获取ToUserID?像这样的东西
how i can get ToUserID on current postion? something like this
@Model [0] .ToUserID
@Model[0].ToUserID
谢谢
推荐答案
您应该可以:
@Model.First().ToUserID
请注意,您可能想先检查可枚举中是否有任何项目,就像没有一样,然后First()将返回null
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
(注意,因为您的问题中有@Model [0],所以我假设您正在专门尝试获取第一个值.我可能杆子的末端错了,在这种情况下,雅库布的答案应该可以解决您的问题! )
(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!)
这篇关于asp.net mvc 3剃须刀从IEnumerable获取一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文