简体   繁体   中英

Creating simple ViewModel to show Two Tables of Data in One View in MVC 5

I am new to MVC and I am trying to create a ViewModel for a certain View. I am stumped and can't find a simple explanation online of how to make a view that contains two simple tables - one showing a list of all the players in Player.cs and one showing a list of all the seasons in Season.cs. I am able to do this in separate views using EF, but have not been able to create one view that shows both tables. Thank you for helping this newbie to MVC and I hope this will help other beginners out there.

Here are my three models. First is Player.cs

using System;
using System.Collections.Generic;

namespace TheFlyingPig.Models
{
    public class Player
    { 
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public virtual ICollection<Stat> Stats { get; set; }
    }
}

Next model is Season.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;

namespace TheFlyingPig.Models
{
    public class Season
    {
        public int SeasonID { get; set; }
        public string SeasonName { get; set; }

        public virtual ICollection<Stat> Stats { get; set; }
    }
}

Third model is Stat.cs

namespace TheFlyingPig.Models
{
    public class Stat
    {
        public int StatID { get; set; }
        public int SeasonID { get; set; }
        public int PlayerID { get; set; }
        public int Hits { get; set; }

        public virtual Season Season { get; set; }
        public virtual Player Player { get; set; }
    }
}

Here is the ViewModel I am trying to create: TeamStat.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using TheFlyingPig.Models;

namespace TheFlyingPig.ViewModels
{
    public class TeamStat
    {
        public List<Player> Players { get; set; }

        public List<Season> Seasons { get; set; }
    }
}

Here is my controller:

using System.Data;
using System.Data.Entity;
using System.Net;
using TheFlyingPig.Models;
using TheFlyingPig.ViewModels;

namespace TheFlyingPig.Controllers
{
    public class HomeController : Controller
    {
        private SoftballContext db = new SoftballContext();

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult TeamStat()
        {
            var players = db.Players().ToList();
            var seasons = db.Seasons().ToList();

            var view = new TeamStat(players, seasons);
            return View(view);
        }
    }
}

Here is my view: TeamStat.cshtml

@model TheFlyingPig.ViewModels.TeamStat

@{
    ViewBag.Title = "Team Stats";
}

<h2>Team Stats</h2>

@foreach (var player in Model._players) 
{
    <table>
        <thead>
            <tr>
                <th>First Name</th>
                <th>Last Name</th>
            </tr>    
        </thead>
        <tbody>
            <tr>
                <td>@player.FirstName</td>
                <td>@player.LastName</td>
            </tr>
        </tbody>
    </table>
}

<br /><br /><br />

@foreach (var season in Model._seasons) 
{
    <table>
        <thead>
            <tr>
                <th>Season Name</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>@season.SeasonName</td>
            </tr>
        </tbody>
    </table>
}

Firstly, your not currently setting the properties of your view model before returning the view (you do not have a constructor that accepts 2 parameters)

public ActionResult TeamStat()
{
  var players = db.Players().ToList();
  var seasons = db.Seasons().ToList();
  var view = new TeamStat()
  {
    Players = players,
    Seasons = seasons
  };
  return View(view);
}

Secondly, your view is referring to properties _players and _seasons which do not exist. It should be

@foreach (var player in Model.Players) { ....

and

@foreach (var player in Model.Seasons) { ....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM