在ASP .NET MVC中动态添加部分控件

时间:2013-08-08 14:04:57

标签: c# .net asp.net-mvc

我想在数据库中保留“模块”的列表,这些模块实际上是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来使用

 @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");

谢谢!

P.S。为了说清楚,最终的想法是提供给用户可编辑模板(CKEditor),因此用户可以添加任何ASP .NET MVC UserControl名称,即“Gadget1”或“Gadget2”,我们可以动态更改WebPage并显示已经过的所有控件动态添加。

1 个答案:

答案 0 :(得分:1)

感谢@satpal

他提出了答案,然后将其删除。我不知道为什么。但他是对的!

Html.RenderAction((string)@ViewBag.InitializeUserControl); 

这很有用!

相关问题