我想根据用户的选择创建ActionResult
的结构。
我的问题是,它是否可以以更通用的方式完成,或至少以某种方式即兴创作?
我只是为了确保它做得很好。
这是我的代码。
<p>A Partial Control that is initialized on Server-Side</p>
@{
<h2>@ViewBag.InitializeUserControl</h2>
Html.RenderAction("ShowGadget",new { actionName = (string)@ViewBag.InitializeUserControl } );
}
public class HomeController : Controller
{
public ActionResult Index()
{
@ViewBag.InitializeUserControl = "InitializeUserControl2"; // IT GOES FROM A DATABASE
return View(new HomeModel());
}
public ActionResult ShowGadget(string actionName)
{
var gadgetPresenter = new GadgetPresenter();
return gadgetPresenter.GetActionResult(actionName);
}
}
public class GadgetPresenter : Controller
{
public ActionResult GetActionResult(string actionName)
{
if (string.IsNullOrEmpty(actionName))
{
return DefaultUserControl();
}
else
{
if (actionName.Equals("InitializeUserControl"))
{
return InitializeUserControl();
}
else if (actionName.Equals("InitializeUserControl2"))
{
return InitializeUserControl2();
}
else
return DefaultUserControl();
}
}
public ActionResult InitializeUserControl2()
{
ColorModel colorModel = new ColorModel
{
Width = 100,
Height = 100,
RGBColor = "#FF0000"
};
return PartialView("UserControls/ColorBlockUserControl2", colorModel);
}
public ActionResult InitializeUserControl()
{
ColorModel colorModel = new ColorModel
{
Width = 200,
Height = 200,
RGBColor = "#FF0000"
};
return PartialView("UserControls/ColorBlockUserControl", colorModel);
}
public ActionResult DefaultUserControl()
{
return PartialView("UserControls/DummyControl");
}
}
答案 0 :(得分:1)
我想我明白你要去哪里,但我认为你可能会强行喂错。因此,看到这将是您的数据库中的值,您需要确保一些事情:
我认为您需要的模式/技术是移动视图引擎。以Scott Hanselman的Mobile View Engine为例,以MVC3(source)为例。请注意引擎如何查找.mobile.cshtml视图,如果不存在,则返回到简单的.cshtml视图。显然,这已经内置于MVC4中,但该技术可用于各种目的。
您可以根据自己的需要进行调整,查找会话变量或类似内容(Singleton over Session将是我的方法),用于存储与客户相关的数据库值,以获取视图前缀并提供特定于其前缀的不同视图
后备是优雅的,模式是关于约定和注入。这当然是解决问题的方法。无论你是否最终使用这项技术,我希望这会有所帮助。祝你好运,天啊!