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