C# 从数据库读取的MVC数据
我正在做我的MVC应用程序。我已经通过实体框架将其连接到我的数据库。在控制器中,我按如下方式运行视图:C# 从数据库读取的MVC数据,c#,.net,asp.net-mvc,C#,.net,Asp.net Mvc,我正在做我的MVC应用程序。我已经通过实体框架将其连接到我的数据库。在控制器中,我按如下方式运行视图: public ActionResult MyMarks() { ClassDeclarationsDBEntities1 entities=new ClassDeclarationsDBEntities1(); return View(entities.Users.ToList()); } 我的看法是: @using ClassDeclarationsThsesis.Mode
public ActionResult MyMarks()
{
ClassDeclarationsDBEntities1 entities=new ClassDeclarationsDBEntities1();
return View(entities.Users.ToList());
}
我的看法是:
@using ClassDeclarationsThsesis.Models
@using Microsoft.AspNet.Identity
@{
ViewBag.Title = "My Marks";
}
<div>
<h4>Account information</h4>
<hr/>
<dl class="dl-horizontal">
<dt>Name</dt>
<dd>Student's name</dd>
<dd></dd>
<dt>Surname</dt>
<dd>Student's surname</dd>
<dt>Email</dt>
<dd>
@HttpContext.Current.User.Identity.Name
</dd>
</dl>
</div>
@使用ClassDeclarationsThsesis.Models
@使用Microsoft.AspNet.Identity
@{
ViewBag.Title=“我的标记”;
}
帐户信息
名称
学生姓名
姓
学生姓氏
电子邮件
@HttpContext.Current.User.Identity.Name
如何用构造函数中传递的实体的实际数据替换学生的姓名?在视图
列表中添加模型
@使用ClassDeclarationsThsesis.Models
@使用Microsoft.AspNet.Identity
@模型列表
@{
ViewBag.Title=“我的标记”;
}
帐户信息
@foreach(模型中的var学生)
{
名称
@学生姓名
姓
@学生姓
@学生邮箱
@HttpContext.Current.User.Identity.Name
}
将此添加到视图顶部
@model List<Users> // you must resolve the namespace like YourProject.Users
由于控制器请求是用户对象列表
,因此需要接受它的IEnumerable
模型
@model IEnumerable<ClassDeclarationsThsesis.Models.Users>
@using Microsoft.AspNet.Identity
@{
ViewBag.Title = "My Marks";
}
<div>
<h4>Account information</h4>
<hr/>
<table>
<tr>
<th>Name</th>
<th>Sure name</th>
<th>Email</th>
</tr>
foreach(var student in Model)
{
<tr>
<td>@student.Name</td>
<td>@student.SurName</td>
<td>@student.Email</td>
</tr>
}
</table>
@model IEnumerable
@使用Microsoft.AspNet.Identity
@{
ViewBag.Title=“我的标记”;
}
帐户信息
名称
确定名称
电子邮件
foreach(模型中的var学生)
{
@学生姓名
@学生姓
@学生邮箱
}
您应该从数据库构建学生模型,然后将此模型传递给视图,并在视图中呈现所需的字段。您应该阅读ASP.NET MVC的基础知识。例如,如何将模型传递给视图。
@foreach(var user in Model)
{
<dt>Name</dt>
<dd>@user.name</dd>
}
@model IEnumerable<ClassDeclarationsThsesis.Models.Users>
@using Microsoft.AspNet.Identity
@{
ViewBag.Title = "My Marks";
}
<div>
<h4>Account information</h4>
<hr/>
<table>
<tr>
<th>Name</th>
<th>Sure name</th>
<th>Email</th>
</tr>
foreach(var student in Model)
{
<tr>
<td>@student.Name</td>
<td>@student.SurName</td>
<td>@student.Email</td>
</tr>
}
</table>