How can I pass/send a ViewBag to my DisplayTemplates? I can't use a ViewModel for this so I think on ViewBags... For example:

Part of my view:

@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>
有帮助吗?

解决方案

The ViewBag is available in the View without you passing it in. If you add something to ViewBag in the Controller, it will be available in the View.

//Controller

public ActionResult Test()
{
   ViewBag.Message = "Hello World";
   return View();
}

//In The View

@if(!string.IsNullOrEmpty(ViewBag.Message))
{
  <p>ViewBag.Message</p>
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top