Table of Contents
直接显示viewdata中的字符串
后台获取数据
我的数据库中有一个名为T_PROJECT的表,这里用Lambda
代码如下:
- 获取数据
int total = 0;
int id = 0;
int.TryParse(xmid, out id);
IQueryable<T_PROJECT> renwulist = BLLFactory<T_PROJECT>.GetBaseService.NonCacheEntityQuery(1, 1, out total, m => m.ID == id,new OrderByExpression<T_PROJECT, int>(m => m.ID));
foreach (T_PROJECT dto in renwulist)
{
ViewBag.xmname = dto.NAME;
ViewBag.endtime = dto.PLANENDTIME;
ViewBag.detail = dto.DETAIL;
ViewBag.starttime = dto.STARTDATE;
ViewBag.xmid = dto.ID;
}
- 调用方法
DisplayProject(xmid);
前台view显示
<input type="text" class="form-control" id="xmName" name="xmName" value="@ViewBag.xmname">
viewdata中自带html代码
后台获取数据
- 获取数据方法
StringBuilder html = new StringBuilder();
IQueryable<T_USER> userlist = BLLFactory<T_USER>.GetBaseService.EntityQuery(m => m.BUMENID == bumen && m.POWER != 0 && m.POWER != 3 && m.STATE != "离职",
new OrderByExpression<T_USER, int?>(m => m.POWER, true));
foreach (T_USER dto in userlist)
{
html.AppendFormat(@"<input type=""checkbox"" name=""id"" value=""{0}"">{1} ", dto.ID, dto.USERNAME);
}
return html.ToString();
- 调用方法将数据存入viewdata
ViewData["XMAllUser"] = GetAllXMUser();
前台view显示
@{
if (ViewData["XMAllUser"] != null)
{
<span>部门人员:</span> @MvcHtmlString.Create(ViewData["XMAllUser"].ToString())
}
}
显示viewdata中的单一数据
后台数据获取
我的数据库中有一个名为T_Column的表,这里获取其中一条数据显示在view上
代码如下:
var item = db.T_Column.OrderByDescending(x => x.id).FirstOrDefault();
ViewData["allColumn"] = item;
前端view显示代码
@using TsxWeb.Models;
@var abc = ViewData["allColumn"] as T_Column
<tr>
<td>@abc.id</td>
<td>@abc.name</td>
<td>@abc.parent</td>
</tr>
显示viewdata中的List
后台数据获取
var item = db.T_Column.OrderByDescending(x => x.id).ToList();
ViewData["allColumn"] = item;
前端view显示代码
- 方法一
@using TsxWeb.Models;
@{
foreach (T_Column col in ViewData["allColumn"] as List<T_Column>)
{
<tr>
<td>@col.id</td>
<td>@col.name</td>
<td>@col.parent</td>
</tr>
}
}
- 方法二
@foreach(var col in(IEnumerable<TsxWeb.Models.T_Column >)ViewData["allColumn"])
{
<option value="@col.DTBCODE">@col.DTBNAME</option>
}