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");
}
}
처럼 CarsController 클래스 모양에 C# 세부 방법을 무엇? – nityan
내 질문을 편집했습니다 – user1711993
'Details' 액션은 nullable 매개 변수를 허용하지만'id'가 없으면 즉시 BadRequest를 반환합니다. 이 'ActionLink' [override] (http://msdn.microsoft.com/en-us/library/dd493068 (v = vs.118) .aspx)를 사용해 보셨습니까? – Jasen