2014-10-11 33 views
1

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

什麼是您的C#詳細方法您CarsController類是什麼樣子? – nityan 2014-10-11 22:04:15

+0

我編輯了我的問題 – user1711993 2014-10-11 22:16:44

+0

您的'Details'操作接受可爲空的參數,但如果'id'不存在,您立即返回BadRequest。你試過這個'ActionLink' [覆蓋](http://msdn.microsoft.com/en-us/library/dd493068(v = vs.118).aspx)? – Jasen 2014-10-11 22:35:31

回答

1

你需要一個可空參數添加到您的ActionLink,以使CarsController知道導航到詳細方法,像這樣:

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