【发布时间】:2021-10-19 01:23:53
【问题描述】:
我正在创建一个 ASP.NET Core 5 MVC Web 应用程序,但我遇到了问题。
View only display the same value.
I have debug and the controller seem to be ok.
但我不知道为什么视图只显示 9 行并且所有行都相同。
这是控制器中的代码:
public class MatchesController : Controller
{
private ISender _mediator;
public MatchesController(ISender mediator)
{
_mediator = mediator;
}
public IActionResult Index()
{
return View();
}
public async Task<IActionResult> ViewAllMatch()
{
var matchQuery = await _mediator.Send(new GetMatchesDetail());
// ReUse model from Apllication Layer
// Manual Mapping from matches to MatchViewModel
// GetMatchesDetail : IRequest<IEnumerable<MatchesDetail>>
// Manual Mapping IEnumerable<MatchesDetail> =>IEnumerable<MatchViewModel>
MatchViewModel matchesvm = new MatchViewModel();
List<MatchViewModel> retList = new List<MatchViewModel>();
// IEnumerable<MatchesDetail> retList;
foreach (var item in matchQuery)
{
matchesvm.MatchId = item.MatchId;
matchesvm.MatchNumber = item.MatchNumber;
matchesvm.DateMatch = item.DateMatch;
matchesvm.TimeMatch = item.TimeMatch;
matchesvm.MatchYear = item.MatchYear;
matchesvm.SeasonId = item.SeasonId;
matchesvm.SeasonName = item.SeasonName;
matchesvm.Round = item.Round;
matchesvm.Stage = item.Stage;
matchesvm.SubStage = item.SubStage;
matchesvm.HTeam = item.HTeam;
matchesvm.HGoal = item.HGoal;
matchesvm.HTeamCode = item.HTeamCode;
matchesvm.GGoal = item.GGoal;
matchesvm.GTeam = item.GTeam;
matchesvm.GTeamCode = item.GTeamCode;
matchesvm.WinNote = item.WinNote;
matchesvm.Stadium = item.Stadium;
matchesvm.Referee = item.Referee;
matchesvm.Visistors = item.Visistors;
retList.Add(matchesvm);
}
return View(retList);
}
}
这是视图:
@model IEnumerable<MatchViewModel>
@{
ViewData["Title"] = "ViewAllMatch";
string flag1, flag2;
}
<h1>ViewAllMatch</h1>
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.MatchId)
</th>
<th>
@Html.DisplayNameFor(model => model.MatchNumber)
</th>
<th>
@Html.DisplayNameFor(model => model.DateMatch)
</th>
<th>
@Html.DisplayNameFor(model => model.TimeMatch)
</th>
<th>
@Html.DisplayNameFor(model => model.MatchYear)
</th>
<th>
@Html.DisplayNameFor(model => model.SeasonId)
</th>
<th>
@Html.DisplayNameFor(model => model.SeasonName)
</th>
<th>
@Html.DisplayNameFor(model => model.Round)
</th>
<th>
@Html.DisplayNameFor(model => model.Stage)
</th>
<th>
@Html.DisplayNameFor(model => model.SubStage)
</th>
<th>
@Html.DisplayNameFor(model => model.HTeam)
</th>
<th>
@Html.DisplayNameFor(model => model.HTeamCode)
</th>
<th>
@Html.DisplayNameFor(model => model.HGoal)
</th>
<th>
@Html.DisplayNameFor(model => model.GGoal)
</th>
<th>
@Html.DisplayNameFor(model => model.GTeam)
</th>
<th>
@Html.DisplayNameFor(model => model.GTeamCode)
</th>
<th>
@Html.DisplayNameFor(model => model.WinNote)
</th>
<th>
@Html.DisplayNameFor(model => model.Stadium)
</th>
<th>
@Html.DisplayNameFor(model => model.Referee)
</th>
<th>
@Html.DisplayNameFor(model => model.Visistors)
</th>
<th>
@Html.DisplayNameFor(model => model.Status)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.MatchId)
</td>
<td>
@Html.DisplayFor(modelItem => item.MatchNumber)
</td>
<td>
@Html.DisplayFor(modelItem => item.DateMatch)
</td>
<td>
@Html.DisplayFor(modelItem => item.TimeMatch)
</td>
<td>
@Html.DisplayFor(modelItem => item.MatchYear)
</td>
<td>
@Html.DisplayFor(modelItem => item.SeasonId)
</td>
<td>
@Html.DisplayFor(modelItem => item.SeasonName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Round)
</td>
<td>
@Html.DisplayFor(modelItem => item.Stage)
</td>
<td>
@Html.DisplayFor(modelItem => item.SubStage)
</td>
<td class="text-center">
@Html.DisplayFor(modelItem => item.HTeam)
</td>
<td>
@{
flag1 = "/img/Team/"+@item.HTeamCode+".png";
flag2= "/img/Team/" + @item.GTeamCode + ".png";
}
<img src="@flag1" />
@*@Html.DisplayFor(modelItem => item.HTeamCode)*@
</td>
<td>
@Html.DisplayFor(modelItem => item.HGoal)
</td>
<td>
@Html.DisplayFor(modelItem => item.GGoal)
</td>
<td>
@Html.DisplayFor(modelItem => item.GTeam)
</td>
<td>
@Html.DisplayFor(modelItem => item.GTeamCode)
</td>
<td>
@Html.DisplayFor(modelItem => item.WinNote)
</td>
<td>
@Html.DisplayFor(modelItem => item.Stadium)
</td>
<td>
@Html.DisplayFor(modelItem => item.Referee)
</td>
<td>
@Html.DisplayFor(modelItem => item.Visistors)
</td>
<td>
@Html.DisplayFor(modelItem => item.Status)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
</tbody>
</table>
【问题讨论】:
-
请分享您的视图代码和控制器代码,以便我们知道发生了什么。
-
感谢您的建议,我已按您的要求编辑问题。
标签: c# .net-core asp.net-core-mvc cqrs