【问题标题】:How to call different controller action on different button clicks?如何在不同的按钮点击上调用不同的控制器动作?
【发布时间】:2012-09-10 14:40:25
【问题描述】:

我有多个按钮来执行不同的收据报告操作。打印按钮会提示用户下载选项。视图代码如下:

@using (Html.BeginForm())
{ 

<fieldset style="width:525px; padding-top:20px; margin:0 auto;">
    <legend>Select Criteria</legend>

        <center>
            @Html.Partial("_MagazineType")<br /><br />
        </center>

            @Html.Partial("_RadioBtnCriteria")

            @Html.Partial("_ReportExportFormat")<br />  

        <center>                 
            <button type= "submit" id = "status" onclick="ReceiptsStatus();" >Receipts Status Details</button>
            <button type= "submit" id = "duplicate" onclick="NoDuplicate();" >No Duplicate</button>
            <button type= "submit" id = "pr_rcpt" onclick="PrintReceipts()">Print Receipts</button>                         
        </center>          

</fieldset><br />

} 

现在,当点击 Receipt status detail 按钮时,它再次提示下载选项,因为使用了 @using (Html.BeginForm()) 选项。但我只想让控制器动作运行。此外,根据输入的参数,它必须在要调用的不同操作中进行选择。那么如何仅与参数一起运行该按钮的特定方法。说 for daterange 必须传递 from 和 to date 并且只能从 date 传递。

【问题讨论】:

标签: asp.net-mvc controller


【解决方案1】:

在我看来,在同一个视图上有多个按钮调用不同操作方法的最简单方法是用@using (HTML.BeginForm(“actionName”, “controllerName”)) 包围每个按钮,然后你不需要你只需要的 onClick 事件,每个按钮的每个回发都会引导你到指定controllerName中的指定actionName。

这个例子修改了 ASP.Net 网站上的电影教程:

<div style="width:100px; float:left" >
    @using (Html.BeginForm())
    {
    <p>
    <input type="submit" value="Delete" /> 
    @Html.ActionLink("Back to List", "Index")
    </p>
    }
    </div>

    <div style="width:100px; float:left">       
    @using (Html.BeginForm("Index","Movies"))
    {
    <p>
    <input type="submit" value="Cancel" /> 
    </p>
    }
</div>

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-27
    • 2017-05-15
    相关资源
    最近更新 更多