一个干净的解决方案中使用多个在ASP.NET MVC提交按钮 [英] A clean solution to use multiple submit button in ASP.NET MVC

查看:76
本文介绍了一个干净的解决方案中使用多个在ASP.NET MVC提交按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了一些类似的问题,我喜欢用MultipleButtonAttribute在这里找到了解决办法:<一href=\"http://stackoverflow.com/questions/442704/how-do-you-handle-multiple-submit-buttons-in-asp-net-mvc-framework\">How你处​​理多个提交按钮在ASP.NET MVC框架?结果
但是,我想出了另一种解决办法,我想我与社区分享。

I found some similar questions and I like the solution with the "MultipleButtonAttribute" found at here: How do you handle multiple submit buttons in ASP.NET MVC Framework?
But I come up with another solution and I thought I share it with the community.

推荐答案

所以首先我作出这样的处理传入的请求ModelBinder的。结果
我已到了一个限制。输入/按钮元素标识和名称必须是CMD。

So first of all I make a ModelBinder that handle the incoming request.
I have to made a restriction. input/button element id and name must be a prefix of "cmd".

  public class CommandModelBinder<T> : IModelBinder
  {
    public CommandModelBinder()
    {
      if (!typeof(T).IsEnum)
      {
        throw new ArgumentException("T must be an enumerated type");
      }
    }

    public object BindModel(System.Web.Mvc.ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
      string commandText = controllerContext.HttpContext.Request.Form.AllKeys.Single(key => key.StartsWith("cmd"));
      return Enum.Parse(typeof (T), commandText.Substring(3));
    }
  }

当然,这是可以改变的,或通过App_Start的web.config中使其向配置。结果
我做的下一件事是一个扩展的HtmlHelper产生必要的HTML标记:

Of course it can be changed or make it to configurable via web.config of App_Start.
The next thing I make is a HtmlHelper extension to generate the necessary HTML markup:

public static MvcHtmlString CommandButton<T>(this HtmlHelper helper, string text, T command)
{
  if (!command.GetType().IsEnum) throw new ArgumentException("T must be an enumerated type");
  string identifier = "cmd" + command;
  TagBuilder tagBuilder = new TagBuilder("input");
  tagBuilder.Attributes["id"] = identifier;
  tagBuilder.Attributes["name"] = identifier;
  tagBuilder.Attributes["value"] = text;
  tagBuilder.Attributes["type"] = "submit";
  return new MvcHtmlString(tagBuilder.ToString());
}

它仍然是一个技术演示所以HTML属性和其他超级重载等着你在自己的发展。结果
现在,我们必须做出一些枚举尝试我们的code。它们可以是一般的或特定的控制器:

It's still a tech demo so the html attribute and the other super overloads waiting for you to develop on your own.
Now we have to make some enumerations to try our code. They can be general or controller specific:

  public enum IndexCommands
  {
    Save,
    Cancel
  }

  public enum YesNo
  {
    Yes,
    No
  }  

现在配对的粘合剂枚举。我这样做是在App_Start文件夹不同的文件。 ModelBinderConfig。

Now pair the enumerations with the binders. I do it in different file in the App_Start folder. ModelBinderConfig.

  ModelBinders.Binders.Add(typeof(IndexCommands), new CommandModelBinder<IndexCommands>());
  ModelBinders.Binders.Add(typeof(YesNo), new CommandModelBinder<YesNo>());

现在我们成立了一切之后,拍动作尝试codeS。我一直是这么简单:

Now after we set up everything, make an action to try the codes. I kept it simple so:

public ActionResult Index()
{
  return View();
}

[HttpPost]
public ActionResult Index(IndexCommands command)
{
  return View();
}

和我的看法是这样的:

@using (Html.BeginForm())
{
  @Html.CommandButton("Save", IndexCommands.Save)
  @Html.CommandButton("Cancel", IndexCommands.Cancel)
}

希望这有助于保持你的code清晰,类型安全性和可读性。

Hope this helps to keep your code clear, type safe and readable.

这篇关于一个干净的解决方案中使用多个在ASP.NET MVC提交按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆