我想在数据库中保留“模块”列表,这些模块实际上是 ASP .NET MVC 的部分视图。
是否可以动态定义?
HTML
<p>A Partial Control</p>
@Html.Partial("UserControls/ColorBlockUserControl", new ColorModel())
<hr />
<p>A Partial Control that is initialized on Server-Side</p>
@{
Html.RenderAction("InitializeUserControl");
}
C#
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new HomeModel());
}
public ActionResult InitializeUserControl()
{
ColorModel colorModel = new ColorModel
{
Width = 200,
Height = 200,
RGBColor = "#FF0000"
};
return PartialView("UserControls/ColorBlockUserControl", colorModel);
}
}
我假设使用 ViewBag 来使用 like
@ViewBag.InitializeUserControl = "InitializeUserControl"; // It goes from the database and can be ANY name
Html.RenderAction(@ViewBag.InitializeUserControl);
但它不工作......
我希望你有定义它的想法
Html.RenderAction("I need here the dynamic var");
谢谢!
PS 为了清楚起见,最终的想法是提供给用户可编辑模板(CKEditor),以便用户可以添加任何 ASP .NET MVC UserControl 名称,即“Gadget1”或“Gadget2”,我们能够动态更改网页并显示所有具有被动态添加。