Home > Enterprise >  View in ASP.NET Core 5 MVC display as required
View in ASP.NET Core 5 MVC display as required

Time:10-22

I am creating an ASP.NET Core 5 MVC web app and I have a problem.

View only display the same value.

I have debug and the controller seem to be ok.

But I don't have any idea why the view is only display 9 row and all rows are the same.

Here is the code in the controller:

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);
    }
}
    

And here is the view:

@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>

CodePudding user response:

The reason why you always get the same value is your MatchViewModel is new outside the foreach method. This means you always added the same MatchViewModel and its value has been changed during each foreach. This will make the List view model always added the same viewmodel like your view shows always the same row.

I suggest you could try below codes:

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>
}

CodePudding user response:

You have bug in your code, you should create a new MatchViewModel instance for each item inside of the loop

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

but you can use Linq instead foreach loop

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

IMHO no much sense to convert IEnumerable of MatchesDetail => IEnumerable of MatchViewModel since they have the same property names and you use them only to display data. You can use matchQuery directly

  return View(matchQuery);

and view

@model IEnumerable<MatchesDetail>
  • Related