Pergunta

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>
Foi útil?

Solução

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>
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top