[编辑]
重新itterate我在其他答案的评论点:当您绑定的逻辑在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动作选择代码:Asp.Net Mvc action selector
实质上你需要一个动作选择器类:
/// <summary>
/// AcceptParameterAttribute to enable submit buttons to execute specific action methods.
/// </summary>
public class AcceptParameterAttribute : ActionMethodSelectorAttribute
{
/// <summary>
/// Gets or sets the value to use in submit button to identify which method to select. This must be unique in each controller.
/// </summary>
public string Action { get; set; }
/// <summary>
/// Determines whether the action method selection is valid for the specified controller context.
/// </summary>
/// <param name="controllerContext">The controller context.</param>
/// <param name="methodInfo">Information about the action method.</param>
/// <returns>true if the action method selection is valid for the specified controller context; otherwise, false.</returns>
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){
...
}
切换动作是脏 - 这是一个更清洁的方式。我觉得也更自然。
这里是该代码与列表(数据绑定)支持的更新版本:http://blogs.sonatribe.com/wayne/2011/06/15/171/ – iwayneo 2011-06-15 07:53:38
我试过这个......但我是得到一个错误信息像“Sample.Controllers.AcceptParameterAttribute”不包含一个构造函数,它带有1个参数“ – san 2011-06-15 09:19:08
ooops抱歉!我的错误 - 你需要使用可选的args方法:[AcceptParameter(Action =“Edit”)] – iwayneo 2011-06-15 09:21:11