C# ActionResult选择器和设计模式
我想根据用户的选择创建一个类似于C# ActionResult选择器和设计模式,c#,asp.net-mvc,design-patterns,actionresult,C#,Asp.net Mvc,Design Patterns,Actionresult,我想根据用户的选择创建一个类似于ActionResult的结构 我的问题是,它是否可以用一种更通用的方式来完成,或者至少是以某种方式即兴完成 我只是想确保它做得好 这是我的密码 <p>A Partial Control that is initialized on Server-Side</p> @{ <h2>@ViewBag.InitializeUserControl</h2> Html.RenderAction("ShowGad
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");
}
}
在服务器端初始化的部分控件
@{
@ViewBag.InitializeUserControl
RenderAction(“ShowGadget”,新的{actionName=(string)@ViewBag.InitializeUserControl});
}
公共类HomeController:控制器
{
公共行动结果索引()
{
@ViewBag.InitializeUserControl=“InitializeUserControl2”//它来自数据库
返回视图(新HomeModel());
}
公共ActionResult ShowGadget(字符串actionName)
{
var gadgetPresenter=新的gadgetPresenter();
返回gadgetPresenter.GetActionResult(actionName);
}
}
公共类GadgetPresenter:控制器
{
公共ActionResult GetActionResult(字符串actionName)
{
if(string.IsNullOrEmpty(actionName))
{
返回DefaultUserControl();
}
其他的
{
if(actionName.Equals(“InitializeUserControl”))
{
返回InitializeUserControl();
}
else if(actionName.Equals(“InitializeUserControl2”))
{
返回InitializeUserControl2();
}
其他的
返回DefaultUserControl();
}
}
public ActionResult InitializeUserControl2()
{
ColorModel ColorModel=新的ColorModel
{
宽度=100,
高度=100,
RGBColor=“#FF0000”
};
返回PartialView(“UserControls/ColorBlockUserControl2”,colorModel);
}
public ActionResult InitializeUserControl()
{
ColorModel ColorModel=新的ColorModel
{
宽度=200,
高度=200,
RGBColor=“#FF0000”
};
返回PartialView(“UserControls/ColorBlockUserControl”,colorModel);
}
公共操作结果DefaultUserControl()
{
返回PartialView(“UserControls/DummyControl”);
}
}
我想我理解你的意图,但我认为你可能是在强制输入错误的模式。因此,鉴于这将是数据库中的一个值,您需要确保以下几点:
回退是优雅的,模式是关于约定和注入的。这当然是我解决你问题的方法。我希望这对你有所帮助,不管你最终是否使用了这种技术。祝你好运,万事如意 这没有任何意义。您的操作将返回一个ActionResult。为什么要创建一个单独的操作来获取第一个操作的结果并返回它?