문제

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