我正在开发一个 MVC,它向用户显示请求列表。该模型看起来像这样:
public class RequestModel
{
public string Status { get; set; }
}
Controller 看起来像这样:
public ActionResult Index()
{
return View(db.Requests.ToList());
}
我希望索引 View 页面显示状态为“待定”的请求列表,我已尝试使用以下代码:
<table class="table">
@foreach (var item in Model)
{
<tr>
<td>
@if (modelItem => item.Status == "Pending")
{
@Html.DisplayFor(modelItem => item.Status)
}
</td>
</tr>
}
</table>
但是,这给了我以下错误:
Cannot convert lambda expression to type "bool" because it is not a delegate type.
我不确定这个错误意味着什么以及如何解决它。如果有人有任何帮助或建议,我将不胜感激。
最佳答案
你的情况应该是这样的:
@if (item.Status == "Pending")
您正在尝试使用 lambda 表达式 (modelItem => item.Status == "Pending"
),而您应该使用简单的条件。
关于c# - 无法将 lambda 表达式转换为类型 'bool' 因为它不是委托(delegate)类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45708569/