0

これは宿題であり、同様の質問を調べましたが、例から構文がどうあるべきかを正確に知るにはコードが難しすぎます。これは、Visual Studio、C# の ASP.NET MVC アプリです。これは、実行すると得られるものです。
ここに画像の説明を入力

「編集」または「詳細」をクリックすると、自転車を編集したり、詳細を入力したりできるページに移動したいと思います。現在、元の課題のインデックス ページに移動していますが、これはこれとは関係ありませんが、教授はすべての課題を 1 つのプロジェクトにまとめたいと考えています。

ソリューション エクスプローラー

ここに私の 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>
4

1 に答える 1

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 }
于 2013-09-30T11:43:34.220 に答える