如何显示所选汽车的详细信息视图

本文关键字:汽车 详细信息 视图 何显示 显示 | 更新日期: 2023-09-27 18:26:13

我是ASP.NET MVC 5的新手。

我已经生成了车库的视图详细信息。

@model TestApp.Models.Garage
@{
    ViewBag.Title = "Details";
}
<h2>Details</h2>
<div>
    <h4>Category</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.Name)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.Name)
        </dd>
        <dt>
            @Html.DisplayNameFor(model => model.Description)
        </dt>
        <dd>
            @Html.DisplayFor(model => model.Description)
        </dd>
    </dl>
    <hr />
    <div>Cars of  garage</div>
    <div>
        @foreach (var item in Model.Cars)
        {
            <li>@Html.ActionLink(@item.Name, "Details", "Cars")</li> // should be correct code line
        }
    </div>
    <hr />
</div>
<p>
    @Html.ActionLink("Edit", "Edit", new { id = Model.GarageID }) |
    @Html.ActionLink("Back to List", "Index") | 
    @Html.ActionLink("Add Car", "Create", "Cars")
</p>

我想显示车库的汽车列表,用户可以点击一些汽车并查看这辆车的详细信息。但我找不到@Html.ActionLink(@item.Name,"Details","Cars")方法的正常接口。

public class CarsController : Controller
{
    private TestDBContext db = new TestDBContext();
    public async Task<ActionResult> Index()
    {
        var cars = db.Cars.Include(p => p.Category);
        return View(await cars.ToListAsync());
    }
    public async Task<ActionResult> Details(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Cars car = await db.Cars.FindAsync(id);
        if (car == null)
        {
            return HttpNotFound();
        }
        return View(cars);
    }
    public ActionResult Create()
    {
        ViewBag.GarageID = new SelectList(db.Garages, "GarageID", "Name");
        return View();
    }
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Create([Bind(Include="CarID,Name,Description, GarageID")] Car car)
    {
        if (ModelState.IsValid)
        {
            db.Cars.Add(car);
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }
        ViewBag.GarageID = new SelectList(db.Garages, "GarageID", "Name", car.GarageID);
        return View(car);
    }
    public async Task<ActionResult> Edit(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Car car = await db.Cars.FindAsync(id);
        if (car == null)
        {
            return HttpNotFound();
        }
        ViewBag.GarageID = new SelectList(db.Garages, "GarageID", "Name", car.GarageID);
        return View(car);
    }
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Edit([Bind(Include="CarID,Name,Description,GarageID")] Car car)
    {
        if (ModelState.IsValid)
        {
            db.Entry(car).State = EntityState.Modified;
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }
        ViewBag.GarageID = new SelectList(db.Garages, "GarageID", "Name", car.GarageID);
        return View(car);
    }
    public async Task<ActionResult> Delete(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Car car = await db.Cars.FindAsync(id);
        if (car == null)
        {
            return HttpNotFound();
        }
        return View(car);
    }
    [HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> DeleteConfirmed(int id)
    {
        Cars car = await db.Cars.FindAsync(id);
        db.Cars.Remove(car);
        await db.SaveChangesAsync();
        return RedirectToAction("Index");
    }
}

如何显示所选汽车的详细信息视图

您需要在ActionLink中添加一个可为null的参数,以便CarsController知道如何导航到Details方法,如下所示:

@Html.ActionLink(item.Name, "Details", "Cars", new { id = item.Id }, null)