【问题标题】:View in ASP.NET Core 5 MVC display as required根据需要在 ASP.NET Core 5 MVC 显示中查看
【发布时间】: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


【解决方案1】:

您总是获得相同值的原因是您的 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>
}

【讨论】:

    【解决方案2】:

    您的代码中有错误,您应该为循环内的每个项目创建一个新的 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多