我想ActionResult
根据用户的选择创建类似 's 的结构。
我的问题是它是否可以以更通用的方式完成,或者至少以某种方式即兴创作?
我只是为了确保它做得很好。
这是我的代码。
<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");
}
}