Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在ASP.NETMVC中动态添加部分控件_C#_.net_Asp.net Mvc - Fatal编程技术网

C# 在ASP.NETMVC中动态添加部分控件

C# 在ASP.NETMVC中动态添加部分控件,c#,.net,asp.net-mvc,C#,.net,Asp.net Mvc,我想在数据库中保留“模块”列表,这些模块实际上是ASP.NETMVC的部分视图 可以动态地定义它吗 HTML <p>A Partial Control</p> @Html.Partial("UserControls/ColorBlockUserControl", new ColorModel()) <hr /> <p>A Partial Control that is initialized on Server-Side</p>

我想在数据库中保留“模块”列表,这些模块实际上是ASP.NETMVC的部分视图

可以动态地定义它吗

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");
}
我假设使用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用户控件名称,即“Gadget1”或“Gadget2”,我们可以动态更改网页并显示所有已动态添加的控件。感谢@satpal

他把答案放进去,然后把它删掉。我不知道为什么。但他是对的

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

这是工作

@Anon是的。。。有一些错误<代码>CS1973:'System.Web.Mvc.HtmlHelper'没有名为'RenderAction'的适用方法,但似乎有一个同名的扩展方法。无法动态调度扩展方法。考虑动态参数或调用扩展方法而不使用扩展方法语法。< /代码>为什么它必须是部分视图而不是简单地由Ajax检索?好的,在您的描述中,您认为它们是部分视图,但是在视图中,您使用的是:html。(@ViewBag.InitializeUserControl)“嗯,我做了。它不像你假设的那样工作。<代码> CS1973:'St.Web.MVC .HTMLHelp'没有可用的方法名为‘Pult',但是它的名字有一个扩展方法。不能动态地扩展扩展方法。考虑在没有扩展方法SY的情况下强制转换动态参数或调用扩展方法。ntax.您的视图中是否有@using语句?
Html.RenderAction((string)@ViewBag.InitializeUserControl);