我通过使用视图模型将我的表数据发送到mvc网页。我的代码如下所示。现在我的任务是通过viewbag或viewdata发送它。任何有激情的程序员请编辑代码,通过viewbag或viedata发送,并给出一个基本的概述,我们如何使用它们,即viewmodel或viebag不同。
提前感谢。
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<div>
<br /><br />
<table style="width: 100%;">
<tr>
<th>UserID</th>
<th>UserName</th>
<th>Adress</th>
</tr>
@foreach(var item in ViewData.Model)
{
<tr>
<td>@item.UserID</td>
<td>@item.Username</td>
<td>@item.Adress</td>
</tr>
}
</table>
</div>
public class DetailController : Controller
{
public ActionResult Index()
{
ViewData.Model = DetailModels.GetData();
return View();
}
}
public class DetailModels
{
public static List<User> GetData()
{
EmployeeEntities context = new EmployeeEntities();
List<User> dt = context.Users.AsEnumerable().ToList();
return dt;
}
}
只包含上面的库
@using ProjectName。模型
@foreach (Users u in Model)
{
<td>@u.UserID</td>
<td>@u.Username</td>
<td>@u.Adress</td>
}
控制器:
public class DetailController : Controller
{
public ActionResult Index()
{
// ViewData.Model = DetailModels.GetData();
ViewData["DetailModels"] = DetailModels.GetData();
return View();
}
}
视图:
@foreach(var item in ViewData["DetailModels"] as DetailModels)
{
<tr>
<td>@item.UserID</td>
<td>@item.Username</td>
<td>@item.Adress</td>
</tr>
}