【问题标题】:How to use ajax link instead of submit button for form?如何使用 ajax 链接而不是表单的提交按钮?
【发布时间】:2013-08-06 01:54:33
【问题描述】:

我认为我有 Ajax 表单:

 @using (Ajax.BeginForm("SearchHuman", "Search", new AjaxOptions(){
 InsertionMode = InsertionMode.Replace,
 UpdateTargetId = "result" }))   

{

<div class="editor-field">
@DescriptionStrings.Lastname: 
@Html.TextBox("LastName")
</div>

<div class="editor-field">
 @DescriptionStrings.Firstname:
 @Html.TextBox("Name")
</div>

//submit button
<input type="submit" value='Start Searching' />

//submit link
 @Ajax.ActionLink("search", "OtherSearch", new{lastName ="",...},  new AjaxOptions()
        {
            InsertionMode = InsertionMode.Replace,
            UpdateTargetId = "tab"
        })

}

我希望仅使用一种表单来提交按钮和 2 个不同搜索(在不同数据库中)的链接。但是如何将表单文本框中的路由值传递给 Ajax.ActionLink?

提前致谢!

【问题讨论】:

    标签: ajax asp.net-mvc ajaxform


    【解决方案1】:

    但是如何将表单文本框中的路由值传递到 Ajax.ActionLink 中?

    你不能。如果要将值发送到服务器,则应使用提交按钮。您可以有 2 个相同形式的提交按钮,它们都提交到相同的控制器操作。然后在此操作中,您可以测试单击了哪个按钮并根据其值执行一个或另一个搜索。

    例子:

    <button type="submit" name="btn" value="search1">Start Searching</button>
    <button type="submit" name="btn" value="search2">Some other search</button>
    

    然后在你的控制器动作中:

    [HttpPost]
    public ActionResult SomeAction(string btn, MyViewModel model)
    {
        if (btn == "search1")
        {
            // the first search button was clicked
        }
        else if (btn == "search2")
        {
            // the second search button was clicked
        }
    
        ...
    }
    

    【讨论】:

      【解决方案2】:

      我们选择的解决方案是实现一个自定义 ActionMethodSelectorAttribute,它允许我们根据其 name 属性来区分按下了哪个按钮。然后,我们用 ActionName 装饰器装饰了许多方法,为它们提供了相同的操作名称(在 BeginFrom 帮助器中指定的那个),然后我们使用自定义的 ActionMethodSelector 装饰器来根据单击的按钮的名称来区分要调用的方法.最终结果是每个提交按钮都会导致调用一个单独的方法。

      一些代码来说明:

      在控制器中:

      [ActionName("RequestSubmit")]
      [MyctionSelector(name = "Btn_First")]
      public ActionResult FirstMethod(MyModel modelToAdd)
      {
          //Do whatever FirstMethod is supposed to do here
      }
      
      [ActionName("RequestSubmit")]
      [MyctionSelector(name = "Btn_Second")]
      public ActionResult SecondMethod(MyModel modelToAdd)
      {
          //Do whatever SecondMethod is supposed to do here
      }
      

      在视图中:

      @using (Ajax.BeginForm("RequestSubmit",.....
      <input type="submit" id="Btn_First" name="Btn_First" value="First"/>
      <input type="submit" id="Btn_Second" name="Btn_Second" value="Second"/>
      

      至于自定义属性:

      public string name { get; set; }
      public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
      {
          var btnName = controllerContext.Controller.ValueProvider.GetValue(name);
          return btnName != null;
      }
      

      【讨论】:

      • 您能否告知我的自定义属性应该扩展什么属性?是 NonActionAttribute 吗?
      • 它应该扩展:ActionMethodSelectorAttribute。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-10
      • 1970-01-01
      • 2014-11-02
      • 2010-11-21
      • 2012-02-10
      • 1970-01-01
      相关资源
      最近更新 更多