Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.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# MVC4-Razor-动态模块内容?_C#_Asp.net_.net_Razor - Fatal编程技术网

C# MVC4-Razor-动态模块内容?

C# MVC4-Razor-动态模块内容?,c#,asp.net,.net,razor,C#,Asp.net,.net,Razor,我一直在学习MVC4和Razor并拥有一个球,但我有一个关于我想要实现的方法的问题: 我有一个页面,上面有一些面板(比如仪表板),还有一组图标,你可以拖放到这些面板上,将模块“安装”到该面板中,并显示其内容。从用户界面的角度来看,这非常棒,现在我正在考虑将其与更丰富的内容联系起来: 我所拥有的: IContentModule 使用Render()方法为每个模块设置一组具体类 处理模块删除事件的控制器和用于获取该模块删除的类实例的激活器 简单的东西实际上,理想情况下,我希望每个模块都负责自己的

我一直在学习MVC4和Razor并拥有一个球,但我有一个关于我想要实现的方法的问题:

我有一个页面,上面有一些面板(比如仪表板),还有一组图标,你可以拖放到这些面板上,将模块“安装”到该面板中,并显示其内容。从用户界面的角度来看,这非常棒,现在我正在考虑将其与更丰富的内容联系起来:

我所拥有的:

  • IContentModule
  • 使用Render()方法为每个模块设置一组具体类
  • 处理模块删除事件的控制器和用于获取该模块删除的类实例的激活器
简单的东西实际上,理想情况下,我希望每个模块都负责自己的内容,但除了从呈现中返回字符串外,还有更好的方法吗,比如,将特定的视图标记指定给特定的具体类,这样我就可以控制呈现的内容,但要以更结构化的方式,想知道这里最好的方法是什么

谢谢你的时间

丹尼

编辑:有点想是否有办法将视图与我的具体类结合起来?e、 g.ViewForum.cshtml绑定到ForumModule.cs,以某种方式实例化视图并从对象的呈现中获取字符串,然后通过字符串将其传递回以插入到我的面板中

面板示例:

<section class="main box droppable" id="MainPanel">
<div class="padding">
Panel 1
</div>
</section>
控制器方法

        [AcceptVerbs(HttpVerbs.Post)]
    public JsonResult AddModule(string id, string returnTo)
    {

        string content = DemoResolve(id);
        try
        {
            IContentModule module  =  (IContentModule)  Activator.CreateInstance(Type.GetType("Foo.Bar.BLLForumModule,Foo.Bar"));
            content  = module.Render();
        }catch(Exception exp)
        {
            throw;
        }
        return Json(new { Target = returnTo, Content = content });
    }

因此,在我有module.Render()的地方,我想我应该得到一个局部视图或其他东西,并根据我手头的对象进行渲染,例如,与我的一位同事合作,提出了一种以某种动态方式将视图和模块绑定在一起的解决方案,如果jQuery帖子返回模块“BLLForumModule”的字符串,我们有以下内容:

    [AcceptVerbs(HttpVerbs.Post)]
    public JsonResult AddModule(string id, string returnTo)
    {

        string content = DemoResolve(id);
        try
        {
            content = RenderView("BLLForumModule");
        }catch(Exception exp)
        {
            throw;
        }
        return Json(new { Target = returnTo, Content = content });
    }

private string RenderView(string moduleName)
    {
        string result = "";

        IContentModule module = (IContentModule)Activator.CreateInstance(Type.GetType("Foo.Bar." + moduleName  +",Foo.Bar"));

        this.ViewData.Model = module;

        using (var sw = new System.IO.StringWriter())
        {

            ViewEngineResult viewResult = ViewEngines.Engines

            .FindPartialView(this.ControllerContext, moduleName);

            var viewContext = new ViewContext(this.ControllerContext,

            viewResult.View, this.ViewData, this.TempData, sw);

            viewResult.View.Render(viewContext, sw);

            result = sw.GetStringBuilder().ToString();

        }
        return result;
    }
这假设Foo.Bar程序集中有一个类与我们试图加载的视图同名(BLLForumModule.cshtml和Foo.Bar.BLLForumModule.cs)

然后,我从视图中获取呈现的内容,并将其作为JsonResult的内容部分吐回,并将JsonResult的目标部分作为它需要放入的面板的ID吐回


这感觉很好,我想,任何建议或改进欢迎

你能发布一些代码来说明你在做什么吗?很难从你的描述中得到一个准确的画面,在别人说之前,我知道这是一个多余的陷阱,我稍后会正确地写出来:P。。。激活器将连接到传递回的模块的ID(理想情况下,我可能会使用枚举从字符串中获取类型:)
    [AcceptVerbs(HttpVerbs.Post)]
    public JsonResult AddModule(string id, string returnTo)
    {

        string content = DemoResolve(id);
        try
        {
            content = RenderView("BLLForumModule");
        }catch(Exception exp)
        {
            throw;
        }
        return Json(new { Target = returnTo, Content = content });
    }

private string RenderView(string moduleName)
    {
        string result = "";

        IContentModule module = (IContentModule)Activator.CreateInstance(Type.GetType("Foo.Bar." + moduleName  +",Foo.Bar"));

        this.ViewData.Model = module;

        using (var sw = new System.IO.StringWriter())
        {

            ViewEngineResult viewResult = ViewEngines.Engines

            .FindPartialView(this.ControllerContext, moduleName);

            var viewContext = new ViewContext(this.ControllerContext,

            viewResult.View, this.ViewData, this.TempData, sw);

            viewResult.View.Render(viewContext, sw);

            result = sw.GetStringBuilder().ToString();

        }
        return result;
    }