返回与默认视图不同的视图

时间:2013-09-30 11:32:18

标签: c# asp.net-mvc visual-studio-2012 view controller

这是家庭作业,我查找了类似的问题,但我也面临着代码挑战,无法从示例中确切地了解语法。这是Visual Studio中的ASP.NET MVC应用程序,C#。这是我运行时得到的结果 enter image description here

当我点击“编辑”或“详细信息”时,我希望它转到可以编辑自行车或输入详细信息的页面。现在,它将转到原始作业的索引页面,这与此无关,但教授希望我们在一个项目中完成所有作业:

Solution Explorer

这是我的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>

1 个答案:

答案 0 :(得分:0)

你永远不会设置你的BikeID,这需要是唯一的

此外,我还不确定这是为了什么

public int bike { get; set; }
在你的BikeController中尝试显式设置id的

new Bike() { BikeID =  1},
new Bike { Manufacturer = "Nishiki", Gears = 5, Frame = "Road", BikeID = 2 }

同时检查你的路由,在Global.asax中会有

确保设置路由,这应该在您的global.asax或App_Start / RouteConfig.cs中

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }