C# 如何根据行中的数据在项目周围添加红色边框?
我有一个模型,其中包含每个客户机的信息,还有一个字节[]存储它们的图像。我希望在图像周围放置一个边框,前提是该特定客户机存在特定值。例如,如果杰克·斯派洛船长的“状态ID”等于2,则他的照片周围将显示一个红色边框。有人能给我举个例子说明怎么做吗?我试着搜索一个例子或教程,但我没有找到任何相关的,或者我没有正确理解 这是我的控制器:C# 如何根据行中的数据在项目周围添加红色边框?,c#,asp.net-mvc-5,C#,Asp.net Mvc 5,我有一个模型,其中包含每个客户机的信息,还有一个字节[]存储它们的图像。我希望在图像周围放置一个边框,前提是该特定客户机存在特定值。例如,如果杰克·斯派洛船长的“状态ID”等于2,则他的照片周围将显示一个红色边框。有人能给我举个例子说明怎么做吗?我试着搜索一个例子或教程,但我没有找到任何相关的,或者我没有正确理解 这是我的控制器: // GET: Client public ActionResult Index(string searchString) {
// GET: Client
public ActionResult Index(string searchString)
{
var clients = from z in db.client__information
select z;
if (!String.IsNullOrEmpty(searchString))
{
clients = clients.Where(n => n.FirstName.Contains(searchString));
}
var avatar = (from d in db.client__information
select d).ToList();
return View(clients);
}
这是我的索引页:
@model IEnumerable<MedPassFinal.Models.dbTables.client__information>
@{
ViewBag.Title = "Nurse - Manage Clients";
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
</head>
<body>
<br />
@Html.ActionLink("Create New", "Create")
@using (Html.BeginForm("Index", "Client", FormMethod.Get))
{
<p>
Search by First Name: @Html.TextBox("searchString") <br />
<input type="submit" value="Search" />
</p>
}
<style>
th, td {text-align: center;}
</style>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Content)
</th>
@*...more Display items continue....*@
@foreach (var item in Model)
{
<tr>
<td>
@{
var base64 = Convert.ToBase64String(item.Content);
var imgsrc = string.Format("data:image/gif;base64,{0}", base64);
}
<img src='@imgsrc' style="max-width:120px;max-height:120px"/>
</td>
<td>
@Html.DisplayFor(modelItem => item.SID.StatusDescription)
</td>
....more items continued.....
@model IEnumerable
@{
ViewBag.Title=“护士-管理客户”;
}
@ActionLink(“新建”、“创建”)
@使用(Html.BeginForm(“Index”、“Client”、FormMethod.Get))
{
按名字搜索:@Html.TextBox(“搜索字符串”)
}
th,td{text align:center;}
@DisplayNameFor(model=>model.Content)
@*…更多显示项目继续*@
@foreach(模型中的var项目)
{
@{
var base64=Convert.tobase64字符串(item.Content);
var imgsrc=string.Format(“数据:image/gif;base64,{0}”,base64);
}
@DisplayFor(modelItem=>item.SID.StatusDescription)
……更多项目继续。。。。。
我将在何处进行更改或添加代码,以便根据Status Id的数据值为照片创建边框?使用您想要的样式创建css类
.red-status {
border: 2px solid red;
}
现在可以使用三元运算符有条件地为元素添加此css类。例如,如果要添加到图像
<img src='@imgsrc' class='@(item.StatusId==2 ?"red-status":"")' />
现在您可以为每个状态定义css类
.myimg-active
{
border: 2px solid green;
}
.myimg-closed
{
border: 2px solid red;
}
只需在视图中使用
if(item.StatusId==2)
就可以用边框设置
元素的样式我该如何在“”元素内部使用“if”?我假设使用“style=”会有什么语法?正如Shyju的回答(但应用于
元素,而不是
元素)谢谢。如果我想在整行(包括照片、名称等)周围显示一个红色边框,而不是该客户端的单个对象,这可能吗?啊,没关系。我现在明白了,不是将其添加到“”中,而是将其添加到“”
.myimg-active
{
border: 2px solid green;
}
.myimg-closed
{
border: 2px solid red;
}