4

我正在尝试使用 Visual Studio 2010 学习 C# MVC4。

我想要做的是在某人的用户名上选择的 1 个视图上获得 2 个模型的完整输出。

所以如果我去 /Teams/Details/1 我会从模型球员和球队那里得到所有的信息。

楷模:

    public class Players
{
    public int ID { get; set; }
    public string Name { get; set; }
    public int price { get; set; }
    public string coach { get; set; }
    public string Team { get; set; }
}

    public class Teams
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string coach { get; set; }
}

控制器

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using voetbal.Models;

namespace voetbal.Controllers
{
    public class TeamsController : Controller
    {
        private TeamsDBContext db = new TeamsDBContext();

        //
        // GET: /Teams/

        public ActionResult Index()
        {
            return View(db.Teams.ToList());
        }

        //
        // GET: /Teams/Details/5

        public ActionResult Details(int id = 0)
        {
            Teams teams = db.Teams.Find(id);
            if (teams == null)
            {
                return HttpNotFound();
            }
            return View(teams);
        }

        //
        // GET: /Teams/Create

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

        //
        // POST: /Teams/Create

        [HttpPost]
        public ActionResult Create(Teams teams)
        {
            if (ModelState.IsValid)
            {
                teams.coach = User.Identity.Name;
                db.Teams.Add(teams);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(teams);
        }

        //
        // GET: /Teams/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Teams teams = db.Teams.Find(id);
            if (teams == null)
            {
                return HttpNotFound();
            }
            return View(teams);
        }

        //
        // POST: /Teams/Edit/5

        [HttpPost]
        public ActionResult Edit(Teams teams)
        {
            if (ModelState.IsValid)
            {
                db.Entry(teams).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(teams);
        }

        //
        // GET: /Teams/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Teams teams = db.Teams.Find(id);
            if (teams == null)
            {
                return HttpNotFound();
            }
            return View(teams);
        }

        //
        // POST: /Teams/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Teams teams = db.Teams.Find(id);
            db.Teams.Remove(teams);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}

我绝对知道如何完成这项工作,我尝试了一个视图模型类,但我不知道如何获取有关某人教练姓名的信息。

我期待着您的回复。

4

1 回答 1

1

有很多方法可以给这只猫剥皮,但你必须首先看到的一件事是,你甚至没有发回这两种模型的一个实例。如果您检查Details操作的代码:

public ActionResult Details(int id = 0)
{
    Teams teams = db.Teams.Find(id);
    if (teams == null)
    {
        return HttpNotFound();
    }
    return View(teams);
}

显然,我们发回的只是Teams模型的一个实例。因此,让我们检查一下我们拥有的选项。考虑以下代码:

public ActionResult Details(int id = 0)
{
    Teams teams = db.Teams.Find(id);
    if (teams == null)
    {
        return HttpNotFound();
    }

    // get the list of players into a List<Players>
    var players ...

    var model = new Tuple<Teams, List<Players>>(teams, players);

    return View(model);
}

现在在视图中,我们可以将模型设置为此(请注意,您需要完全限定TeamsPlayers在 @model 声明中):

@model Tuple<Teams, List<Players>>

从那里我们可以做类似的事情:

Model.Item1.Name

或者:

Model.Item2[0].Name;
于 2013-01-04T12:39:17.787 回答