这是家庭作业,我查找了类似的问题,但我对代码的挑战太大,无法从示例中确切知道语法应该如何。这是 Visual Studio、C# 中的 ASP.NET MVC 应用程序。这是我运行它时得到的。
当我单击“编辑”或“详细信息”时,我希望它转到可以编辑自行车或输入详细信息的页面。现在,它会转到原始作业的索引页面,这与此无关,但教授希望我们所有的作业都在一个项目中:
这是我的 BikeController 代码:
using MvcApplication3.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication3.Controllers
{
public class BikeController : Controller
{
//
// GET: /Bike/
List<Bike> bikes;
public BikeController()
{
if (bikes == null)
{
bikes = new List<Bike> {
new Bike(),
new Bike { Manufacturer = "Nishiki", Gears = 5, Frame = "Road" }
};
}
}
public ActionResult Index()
{
return View(this.bikes);
}
private ActionResult View(Func<object> func)
{
throw new NotImplementedException();
}
//
// GET: /Bike/Details/5
public ActionResult Details(int id)
{
var currentBikes = bikes[id];
return View(currentBikes);
}
//
// GET: /Bike/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Bike/Create
[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
Bike b = new Bike
{
Manufacturer = collection["Manufacturer"], Gears = int.Parse(collection["Gears"]), Frame = collection["Frame"]
};
bikes.Add(b);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /Bike/Edit/5
public ActionResult Edit(int id)
{
return View(bikes.Where(b => b.BikeID == id).First());
}
//
// POST: /Bike/Edit/5
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
try
{
// TODO: Add update logic here
return RedirectToAction("Bike/Index");
}
catch
{
return View();
}
}
//
// GET: /Bike/Delete/5
public ActionResult Delete(int id)
{
return View();
}
//
// POST: /Bike/Delete/5
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
// TODO: Add delete logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
public int bike { get; set; }
}
}
这是自行车视图中的索引:
@model IEnumerable<MvcApplication3.Models.Bike>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Manufacturer)
</th>
<th>
@Html.DisplayNameFor(model => model.Gears)
</th>
<th>
@Html.DisplayNameFor(model => model.Frame)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Manufacturer)
</td>
<td>
@Html.DisplayFor(modelItem => item.Gears)
</td>
<td>
@Html.DisplayFor(modelItem => item.Frame)
</td>
<td>
@item.BikeID @item.Manufacturer @item.Gears @item.Frame
@Html.ActionLink("Edit", "Edit", new { id=item.BikeID }) |
@Html.ActionLink("Details", "Details", new { id=item.BikeID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.BikeID })
</td>
</tr>
}
</table>
</body>
</html>