Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 将未读项目从控制器传递到视图_Asp.net Mvc 3_Razor - Fatal编程技术网

Asp.net mvc 3 将未读项目从控制器传递到视图

Asp.net mvc 3 将未读项目从控制器传递到视图,asp.net-mvc-3,razor,Asp.net Mvc 3,Razor,如果通话中有任何未读的交互,我想转到我的“视图” 这就像一封电子邮件,如果在一组电子邮件中有一封未读的电子邮件,它将以粗体显示。我正在做类似的事情 我有一个存储库,所以我在做这个 foreach(item in callRepo.All()) { if (item.Interactions.Count(x=>x.Unread==true)>0) { } } return View(callRepo.All()); 一个调用有很多交互,如果有任何未读的交互,

如果通话中有任何未读的交互,我想转到我的“视图”

这就像一封电子邮件,如果在一组电子邮件中有一封未读的电子邮件,它将以粗体显示。我正在做类似的事情

我有一个存储库,所以我在做这个

foreach(item in callRepo.All())
{
    if (item.Interactions.Count(x=>x.Unread==true)>0) 
    {
    }
}
return View(callRepo.All());
一个调用有很多交互,如果有任何未读的交互,我想转到我的视图


这就是我现在的观点:

var CSSclass=“已读”

foreach(callRepo.All()中的项)
{
if(item.Interactions.Count(x=>x.Unread==true)>0{CSSclass=“Unread”}
}

我不想以我的观点来看待这件事

在我的控制器上有这样做的方法吗


我的问题清楚吗?很抱歉英语不好。Tks

您打算调用此存储库方法多少次(到目前为止,我已经计算了3次)

我建议您使用视图模型:

public class MyViewModel
{
    public bool IsRead { get; set; }
    public IEnumerable<Interaction> Interactions { get; set; }
}
你的看法是:

@model IEnumerable<MyViewModel>
<table>
    @Html.DisplayForModel()
</table>
此代码的进一步改进是将视图模型中的
IEnumerable
属性替换为特定的
IEnumerable
,并用于外部化模型和视图模型之间的映射

public ActionResult Index()
{
    var items = callRepo.All();
    var model = items.Select(item => new MyViewModel
    {
        Interactions = item.Interactions,
        IsRead = item.Interactions.Any(x => x.Unread == true)
    });
    return View(model);
}
@model IEnumerable<MyViewModel>
<table>
    @Html.DisplayForModel()
</table>
@model MyViewModel
@{
    var css = model.IsRead ? "read" : "unread";
}
<tr class="@css">
    ...
</tr>