【问题标题】:I need to link a button to a function in a controller class?我需要将按钮链接到控制器类中的函数吗?
【发布时间】:2012-05-31 06:25:29
【问题描述】:

我有一个打印按钮和一个打印方法,我需要将两者联系起来吗?

我需要让打印按钮调用这个方法,public ActionResult Print(int id)

我不确定如何将两者联系起来,我确定有很多选项我只是在寻找最简单的一个,如果我不介意将代码添加到 html 或 jQuery。

可以给我看看。在此先感谢:)

创建到我的按钮:

<input type="button" value="Print" id="btnPrint" /> 

下面是我的控制器:

namespace Contract.Controllers
{

    public class ContractController : Controller
    {

        CompassEntities db = new CompassEntities();

        public ActionResult Index()
        {
            IEnumerable<tbSalesContract> contracts = db.sptbSalesContractsGetForUser(Environment.UserName.Trim() + "_comps").AsEnumerable();

            return View(contracts);
        }

        public ActionResult Print(int id)
        {
            return View(""); // This can be removed and Print code may be added
        }

        public ActionResult Edit(int? id)
        {
            tbSalesContract c = new tbSalesContract();
            c.Suretyship = true;
            if (id != null)
            {
                c = db.tbSalesContracts.Find(id);
            }

            ViewBag.UserName = Environment.UserName.Trim();

            //ContractInstance c = new ContractInstance();       
            return View(c);
        }

已经可以使用 JQuery 设置打印按钮的动作:

$('#btnPrint').click(function () {     
        if ($('#chkFinal').is(':checked')) {
            $(function () {
                $("#PrintDialog").dialog('close');
            });
        }
        else {
            $('#chkFinal').attr('checked', true); // Set finalized. This checkbox is not visible its like a hidden field
            $('#btnSubmit').click(); // Save
        }

我目前拥有的,编译但不带我打印的方法:

// Save, set state to finalized and Print
    $('#btnDialogPrint').click(function () {    
        if ($('#chkFinal').is(':checked')) {
            $(function () {
                $("#PrintDialog").dialog('close');
            });
        }
        else {
            $('#chkFinal').attr('checked', true); // Set finalized. This checkbox is not visible its like a hidden field
            $('#btnSubmit').click(); // Save
        }

    $.ajax({
        url: '@Url.Action("Print","ContractController", new {id = Model.SalesContractId})'
    });
    });

【问题讨论】:

  • 您希望表单提交给操作吗?或者您想在 chkFinal 验证后进行 ajax 调用?
  • 取决于您希望“打印”做什么。您希望它替换整个浏览器窗口吗?您是否希望它更新当前窗口中的某些元素?你想让它为你提供一个文件供你下载吗?
  • @StaffordWilliams 哈哈,虽然我认为 gdoron 打败了我们俩:P
  • @JasonKulatunga。名誉归@gdoron...:)
  • 我想在 if 语句之后运行我的打印调用。必须通过当前 ID 发送

标签: c# jquery asp.net html asp.net-mvc-3


【解决方案1】:
$('#btnPrint').click(function() {
    // Call the controller function, put it where you need it.
    $.ajax({
        url: '@Url.Action("Print","ContractController", new {id = Model.SalesContractId})'
    });


    if ($('#chkFinal').is(':checked')) {
        $(function() {
            $("#PrintDialog").dialog('close');
        });
    }
    else {
        $('#chkFinal').attr('checked', true); // Set finalized. This checkbox is not visible its like a hidden field
        $('#btnSubmit').click(); // Save
    }​
});

【讨论】:

  • 讨论moved here
  • @RobertHarvey。谢谢 当cmets没有结束时我该怎么办?在每条评论中,我确信这将是最后一条,但它没有......
  • 希望大家能看到我的评论和get the hint.
猜你喜欢
  • 2014-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-10
  • 2013-05-01
  • 1970-01-01
  • 2021-04-22
  • 1970-01-01
相关资源
最近更新 更多