.NET MVC 4 응용 프로그램에서 레이아웃을 확장하고 싶습니다.중첩 된 레이아웃에 데이터를 전달하는 방법
_Root.cshtml
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<meta name="viewport" content="width=device-width" />
</head>
<body>
@RenderBody()
</body>
_productView.cshtml
@{
Layout = "~/Layouts/_Root.cshtml";
}
_productListView.cshtml을 : 그래서 내가 아주 간단한 방법으로 중첩 된 레이아웃을 만들었습니다
@{
Layout = "~/Layouts/_Root.cshtml";
}
이제 데이터베이스에서 제품을 가져 와서 _productView.cshtml에서 제품 카테고리 제목을 설정하려고합니다. 예를 들어 전자 제품이라는 제목을 원한다고 말할 수 있습니다.
public ActionResult Index(string id) {
var product= repository.GetById(new Guid(id));
if(product== null) {
throw new HttpException(404, "Not Found");
}
var model = Mapper.Map<IProduct, ProductViewModel>(product);
return View(model);
}
는 내가 뭘 원하는를 얻을 수있다
ProductController : 난 내가 데이터베이스에서 제품을 얻을 때 나는 그것의 뷰 모델을하기 전에이 작업을 수행하고 내 컨트롤러에서 뷰를 반환하려면 제품 유형을 제품에서 사용하고 _productView.cshtml에서 사용하지만 뷰백을 사용하여 수행 할 수 없다는 것을 알고 있습니다. 그럼 내가 어떻게 할 수 있니?
감사 조나단
나는 예를 확장합니다 UPDATE : 내 productView.cshtml
을에서 다음
public ActionResult Index(string id) {
var product= repository.GetById(new Guid(id));
if(product== null) {
throw new HttpException(404, "Not Found");
}
Viewbag.ProductType = product.productType;
var model = Mapper.Map<IProduct, productViewModel>(product);
return View(model);
}
: 그리고 난 내 컨트롤러에서이 작업을 수행 할 경우
을 _productView.cshtml
@{
Layout = "~/Layouts/_Root.cshtml";
}
@Viewbag.productType
현재 상황에서 뷰백이 존재하지 않는다는 컴파일 오류가 발생합니다.
제품에 대한 나의 견해는 다음과 같다 : 이것은 내가 다른 방법으로 그것을 할 수 있는지, 내가 실현하려 노력하지만, 난 그 viewbag이 방법을 사용 할수 없어 읽은 내용입니다
@model producttest.product.Web.ProductViewModel
@{
Layout = "~/Layouts/_productView.cshtml";
ViewBag.Title = @Model.Name;
}
<h1>@Model.Name)</h1>
<section>
<h2>@Model.Description, @Model.Price</h2>
<p>
@Model.ImageUrl<br />
</p>
</section>
?
UPDATE 2 :
가 Viewbag 그냥 오타했다, 여기에 실제 코드 :
Compiler Error Message: CS0103: The name 'ViewBag' does not exist in the current context
Source Error:
Row 3:
Row 4: }
Row 5: @ViewBag.productType;
요구는 –
@합니다. – JMK
@ 데이비드 허스트 그래 미안 예를 업데이트 오타를 먹으 렴을 viewbag 반대로 그것은, ViewBag해야 ViewBag.productType – Jonathan