首页 > 解决方案 > 如何将 Id 赋予 @using (Ajax.BeginForm("Index", "Customer", FormMethod.Post, new AjaxOptions(){})){....}

问题描述

我正在做一个 asp.net mvc 项目,我想在使用以下 jquery 代码提交表单时禁用所有提交按钮:

$(function()
{
  $('#theform').submit(function(){
    $("input[type='submit']", this)
      .val("Please Wait...")
      .attr('disabled', 'disabled');
    return true;
  });
});

我的表格如下:

@using (Ajax.BeginForm("RegisterCustomer", "Customer", FormMethod.Post, new AjaxOptions()
{
    OnSuccess = "success",
    UpdateTargetId = "listUsers"

}))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <div class="form-group has-feedback">
        @Html.EditorFor(model => model.identificationNo, new {htmlAttributes = new {@class = "form-control", placeholder = Html.DisplayNameFor(x => x.identificationNo)}})
        @Html.ValidationMessageFor(model => model.identificationNo, "", new {@class = "text-danger"})
    </div>

    <div class="form-group has-feedback">
        @Html.DropDownListFor(model => model.identificationType, new SelectList(itemsOfIdentificationType, "Key", "Value"), "نوع شناسه را انتخاب نمایید", new {style = "width:315px; height: 30px; padding 5px; margin: 5px 0 6px; background: none repeat scroll 0 0 #FFFFFF; vertical-align:middle;"})
        @Html.ValidationMessageFor(model => model.identificationType, "", new {@class = "text-danger"})
    </div>
        <div class="row">
            <div class="col-xs-12">
                <button class="btn btn-primary btn-block btn-flat" type="submit">Register</button>
            </div>
        </div>
}

我如何将 ID 提供给:

@using (Ajax.BeginForm("Index", "Customer", FormMethod.Post, new AjaxOptions(){})){ /* ... */}

另一个问题是,有没有更好的方法在表单提交时禁用提交按钮,并在表单提交完全没有问题时再次启用按钮?

标签: javascriptc#jqueryasp.net-mvc

解决方案


您调用的方法签名的第 5 个参数Ajax.BeginForm()htmlAttributes对象,您可以像这样提供:

@using (Ajax.BeginForm(
  "Index", 
  "Customer", 
  FormMethod.Post, 
  new AjaxOptions() { /* your options */ },
  new { @Id = "theform" } /* HTML attributes can be placed in this object */
))
{
  // form controls here...
}

推荐阅读