我的控制器代码如下所示:
public class ResultController : Controller
{
private OnlineVotingSystemEntities db = new OnlineVotingSystemEntities();
// GET: Result
public ActionResult result()
{
var vp = (from c in db.Candidates
join v in db.VoterRegs
on c.UserReg equals v.UserRegNo
where c.CandidatePosition == "VP"
orderby c.Vote descending
select new {
UserPhoto = v.UserPhoto,
UserFirstName =v.UserFirstName,
UserRegNo = v.UserRegNo,
Vote = c.Vote
}).ToList();
ViewBag.VP1 = vp;
var gs = (from c in db.Candidates
join v in db.VoterRegs
on c.UserReg equals v.UserRegNo
where c.CandidatePosition == "GS"
orderby c.Vote descending
select new
{
UserPhoto = v.UserPhoto,
UserFirstName = v.UserFirstName,
UserRegNo = v.UserRegNo,
Vote = c.Vote
}).ToList();
ViewBag.GS1 = gs ;
var ags = (from c in db.Candidates
join v in db.VoterRegs
on c.UserReg equals v.UserRegNo
where c.CandidatePosition == "AGS"
orderby c.Vote descending
select new
{
UserPhoto = v.UserPhoto,
UserFirstName = v.UserFirstName,
UserRegNo = v.UserRegNo,
Vote = c.Vote
}).ToList();
ViewBag.AGS1 = ags;
var member = (from c in db.Candidates
join v in db.VoterRegs
on c.UserReg equals v.UserRegNo
where c.CandidatePosition == "Member"
orderby c.Vote descending
select new
{
UserPhoto = v.UserPhoto,
UserFirstName = v.UserFirstName,
UserRegNo = v.UserRegNo,
Vote = c.Vote
}).ToList();
ViewBag.Member1 = member;
return View();
}
}
我的观看页面如下所示:
@using OnlineVoting.Models
@{
ViewBag.Title = "result";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>result</h2>
<div class="container">
<h4>VP</h4>
<table cellpadding="2" cellspacing="2" border="1" class="dataTable">
<tr>
<th>Photo</th>
<th>User Name</th>
<th>Registration Number</th>
<th>Vote</th>
</tr>
@foreach (var item in ViewBag.VP1)
{
<tr>
<td><p><img src="data:image;base64,@Convert.ToBase64String(item.UserPhoto)" style="width:60px; height: 80px" class="VPimage" /></p></td>
<td>@item.UserFirstName</td>
<td>@item.UserRegNo</td>
<td>@item.Vote</td>
</tr>
}
</table>
</div>
<div class="container">
<h4>GS</h4>
<table cellpadding="2" cellspacing="2" border="1">
<tr>
<th>Photo</th>
<th>User Name</th>
<th>Registration Number</th>
<th>Vote</th>
</tr>
@foreach (var item in ViewBag.GS1)
{
<tr>
<td><p><img src="data:image;base64,@Convert.ToBase64String(item.UserPhoto)" style="width:60px; height: 80px" class="VPimage" /></p></td>
<td>@item.UserFirstName</td>
<td>@item.UserRegNo</td>
<td>@item.Vote</td>
</tr>
}
</table>
</div>
<div class="container">
<h4>AGS</h4>
<table cellpadding="2" cellspacing="2" border="1">
<tr>
<th>Photo</th>
<th>User Name</th>
<th>Registration Number</th>
<th>Vote</th>
</tr>
@foreach (var item in ViewBag.AGS1)
{
<tr>
<td><p><img src="data:image;base64,@Convert.ToBase64String(item.UserPhoto)" style="width:60px; height: 80px" class="VPimage" /></p></td>
<td>@item.UserFirstName</td>
<td>@item.UserRegNo</td>
<td>@item.Vote</td>
</tr>
}
</table>
</div>
<div class="container">
<h4>MEMBER</h4>
<table cellpadding="2" cellspacing="2" border="1">
<tr>
<th>Photo</th>
<th>User Name</th>
<th>Registration Number</th>
@*<th>Vote</th>*@
</tr>
@foreach (var item in ViewBag.Member1)
{
<tr>
<td><p><img src="data:image;base64,@Convert.ToBase64String(item.UserPhoto)" style="width:60px; height: 80px" class="VPimage" /></p></td>
<td>@item.UserFirstName</td>
<td>@item.UserRegNo</td>
<td>@item.Vote</td>
</tr>
}
</table>
</div>
&#13;
当我运行此代码时,显示输出:
图3显示了此代码的输出。我收到了一个错误。此错误是:对象不包含UserPhoto的定义。为什么我会遇到这种错误?请帮帮我。
答案 0 :(得分:0)
在您的控制器中,您使用的是匿名对象,但我建议您使用强类型对象,因为您使用的是Viewbag,因此无需在视图中进行转换。喜欢
`
var vp = (from c in db.Candidates
join v in db.VoterRegs
on c.UserReg equals v.UserRegNo
where c.CandidatePosition == "VP"
orderby c.Vote descending
select new CandiateViewModel{
UserPhoto = v.UserPhoto,
UserFirstName =v.UserFirstName,
UserRegNo = v.UserRegNo,
Vote = c.Vote
}).ToList();