获取提交按钮的名称

时间:2012-11-07 16:09:35

标签: jquery asp.net-mvc-4

我在.Net平台上使用MVC 4并在我的视图中使用以下html。您可以看到名称相同,以区分进入Controller时按下哪个按钮以采取适当的操作。

<tr>
    <td>
        <input type="submit" name="submitbutton" id="btnRefresh" value='Refresh' />
    </td>
        @if (Model.ShowGeneratePDFBtn == true)
        {
            <td>
                <input type="submit" name="submitbutton" id="btnGeneratePDF" value='Generate PDF' />
            </td>
        }
</tr>

然而,当按下第二个(“生成PDF”)按钮时,我需要禁用它,以便用户在进行过程中无法再次按下它。如果我禁用它,它不再作为控件出现在html中(这是可以理解的)。

这是js:

$('#btnGeneratePDF').click(function () {
    DisableGeneratePDF();
});

function DisableGeneratePDF() {
    $('#btnGeneratePDF').attr("disabled", true);
}

在Controller中,按下时按钮的值为null而不是“Generate PDF”,因此不会执行正确的操作。

这是Controller中的第一行:

[HttpPost]
public ActionResult ProcessForm(string submitbutton, ViewModelTemplate_Guarantors model, FormCollection collection)

这是视图中的第一行。我也尝试使用FormMethod.Post,但由于我不会去另一个页面,因此AjaxOption会更有效率。

@using (Html.BeginForm("ProcessForm", "Home", new AjaxOptions { HttpMethod = "POST" }))

当我禁用按钮时,如何成功将“生成PDF”值传递给控制器​​?

1 个答案:

答案 0 :(得分:1)

创建隐藏的表单值并将其放在视图模型中。然后在单击事件处理程序中设置该值。

在您的模型中添加以下内容

public class Guarantors
{
    public bool ShouldGeneratePdf { get; set; }
}

在您看来,添加

@Html.HiddenFor(x => x.ShouldGeneratePdf)

到您的JavaScript

$('#btnGeneratePDF').click(function () {
    DisableGeneratePDF();
    $('#ShouldGeneratePdf').val('True');
});

function DisableGeneratePDF() {        
    $('#btnGeneratePDF').attr("disabled", true);
}

应该这样做。