ASP中的视图.. NET Core 5 MVC按需显示



我正在创建一个ASP。. NET Core 5 MVC web应用程序,我有一个问题。

查看只显示相同的值

我已经调试和控制器似乎是好的。

但是我不知道为什么视图只显示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>

你总是得到相同值的原因是你的MatchViewModel在foreach方法之外是新的。这意味着您总是添加相同的MatchViewModel,并且它的值在每次foreach期间都已更改。这将使列表视图模型总是添加相同的视图模型,就像你的视图总是显示相同的行一样。

我建议你可以试试下面的代码:

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>

List<MatchViewModel> retList = new List<MatchViewModel>();
//IEnumerable<MatchesDetail> retList;
foreach (var item in matchQuery)
{
MatchViewModel matchesvm = new MatchViewModel();
#region ManualMapping
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;
#endregion
retList.Add(matchesvm);
}

return View(retList);
//return View(matches); //IEnumerable<MatchesDetail>
}

你的代码中有bug,你应该为循环中的每个项目创建一个新的MatchViewModel实例

foreach (var item in matchQuery)
{
var matchesvm = new MatchViewModel();
matchesvm.MatchId = item.MatchId;
... and so on
}

但是你可以用Linq代替foreach循环

var retList= matchQuery.Select(item=> new MatchViewModel
{
MatchId = item.MatchId;
MatchNumber = item.MatchNumber;
DateMatch = item.DateMatch 
.... and so on
}).ToList();
return View(retList);

转换IEnumerable的MatchesDetail =>IEnumerable的MatchViewModel,因为它们有相同的属性名,你只用它们来显示数据。你可以直接使用matchQuery

return View(matchQuery);

和视图

@model IEnumerable<MatchesDetail>

最新更新