【问题标题】:Return a different view than default返回与默认视图不同的视图
【发布时间】:2013-09-30 11:32:17
【问题描述】:

这是家庭作业,我查找了类似的问题,但我对代码的挑战太大,无法从示例中确切知道语法应该如何。这是 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>

【问题讨论】:

  • 向我们展示创建这些链接的索引的cshtml
  • 对不起,它现在在那里。

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


【解决方案1】:

您永远不会设置您的 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 }

【讨论】:

  • 这是我的模型:命名空间 MvcApplication3.Models { public class Bike { public int BikeID { get;放; } 公共字符串制造商 { 获取;放; } 公共 int 齿轮 { 获取;放; } 公共字符串框架 { 获取;放; } 静态保护的 int 自行车计数;公共自行车() { this.Manufacturer = "Schwinn"; this.Gears = 10; this.Frame = "山"; this.BikeID = 自行车计数;自行车计数++; } } }
  • 好吧,这不是创建索引的好方法,因为每次调用动作时,int bike 都会增加。这每次都会给你不同的ID。现在尝试显式设置 id。
  • 浏览器索引页的Edit url是什么?它应该类似于 /Bike/Edit/1
  • localhost:6045/?action=Edit&controller=Bike&id=0 当我点击为 Schwinn 编辑时。
  • 这有两点不对劲,最重要的是id=0,这个应该是id=1(如果你明确设置了BikeID)2号,看起来你的路由可能没有设置好了,我会更新答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-08
  • 1970-01-01
  • 2019-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-02
相关资源
最近更新 更多