我正在尝试使用 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);
}
}
}
我绝对知道如何完成这项工作,我尝试了一个视图模型类,但我不知道如何获取有关某人教练姓名的信息。
我期待着您的回复。