DisplayTemplate中的Viewbag

时间:2013-12-18 16:02:58

标签: html asp.net asp.net-mvc-4 razor viewbag

如何将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>

1 个答案:

答案 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>
}