如何将ViewBag传递/发送到DisplayTemplates?我不能使用ViewModel,所以我认为ViewBags ... 例如:
我的部分观点:
@Html.DisplayFor(model => model.Car)
Car.cshtml(displayTemplate):
<tr>
<td>
@Html.TextBoxFor(model => model.Name, new { Name = modelFieldInitialName + "Name", id = modelFieldInitialId + "Name" })
@Html.ValidationMessageFor(model => model.Name)
</td>
<td>
@Html.DropDownListFor(model => model.Brand, new SelectList(ViewBag.Brands, "Description", "Description"), "", new { Name = modelFieldInitialName + "Brand", id = modelFieldInitialId + "Brand" })
@Html.ValidationMessageFor(model => model.Brand)
</td>
</tr>
答案 0 :(得分:1)
ViewBag
在视图中可用,而不会将其传入。如果您在控制器中向ViewBag
添加内容,它将在视图中显示。
//Controller
public ActionResult Test()
{
ViewBag.Message = "Hello World";
return View();
}
//In The View
@if(!string.IsNullOrEmpty(ViewBag.Message))
{
<p>ViewBag.Message</p>
}