[编辑]
为了重申我在其他答案评论中的观点:当你将逻辑绑定到C#中按钮的值时,你将你的C#代码与该语言绑定在一起。
想象一下,在英文版中,“保存”按钮的文本为:
<input type="submit" value="Insert" name='button' />
在您的代码中,您将使用该值进行切换:
public ActionResult Index(string button)
{
switch (button)
{
case "Insert": ...
case "Update": ...
case "Delete": ...
}
return View();
}
现在,当该表单被查看为另一种语言时,你认为会发生什么?!?!
以下是威尔士语的HTML输出:
<input type="submit" value="Mewnosod" name='button' />
并且德国:
<input type="submit" value="Einfügen" name='button' />
这怎么可能运行?!
全球化不是个单独的问题!!!
如果您使用此方法,您的操作将如下所示:
public ActionResult Index(string button)
{
switch (button)
{
case "Insert": ...
case "Update": ...
case "Delete": ...
case "Einfügen": ...
case "Mewnosod": ....
.... a load of other languages for each action type -
}
return View();
}
请认真对待...
[/编辑]
这是我的MVC Action Selector代码:Asp.Net Mvc action selector
本质上,您需要一个动作选择器类:
public class AcceptParameterAttribute : ActionMethodSelectorAttribute
{
public string Action { get; set; }
public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
{
if (controllerContext == null)
{
throw new ArgumentNullException("controllerContext");
}
var req = controllerContext.RequestContext.HttpContext.Request;
return req.Form.AllKeys.Contains(this.Action);
}
}
这个功能根据你给按钮的名字来运作。
你可以装饰这些动作:
[AcceptParameter(Action = "Edit")]
public ActionResult Person_Edit(PersonViewModel model){
...
}
在操作中进行切换是不干净的 - 这种方法更加清晰。我认为这样更自然。