【问题标题】:How to show details View of selected Car?如何显示所选汽车的详细信息视图?
【发布时间】:2014-12-06 19:42:55
【问题描述】:

我是 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");
    }
}

【问题讨论】:

  • CarsController 类上的 C# Details 方法是什么样的?
  • 我编辑了我的问题
  • 您的 Details 操作接受可为空的参数,但如果 id 不存在,您会立即返回 BadRequest。你试过这个ActionLinkoverride吗?

标签: c# asp.net-mvc


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    • 2016-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多