我正在創建一個 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 >
<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 >
@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>
uj5u.com熱心網友回復:
你總是得到相同值的原因是你的 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>
}
uj5u.com熱心網友回復:
您的代碼中有錯誤,您應該為回圈內的每個專案創建一個新的 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);
恕我直言,轉換 MatchesDetail 的 IEnumerable => MatchViewModel 的 IEnumerable 沒有多大意義,因為它們具有相同的屬性名稱,并且您僅使用它們來顯示資料。您可以直接使用 matchQuery
return View(matchQuery);
并查看
@model IEnumerable<MatchesDetail>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/330839.html
標籤:网络 .net核心 asp.net-core-mvc cqrs
上一篇:如何將不和諧機器人連接到網站
