2012-05-31 18 views
0

我有打印按鈕和打印方法,我需要鏈接兩個?我需要將按鈕鏈接到控制器類中的函數?

我需要使打印按鈕調用此方法,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); 
     } 

Action的打印按鈕已經可以體進行設定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})' 
    }); 
    }); 
+0

你想要的形式提交給行動?或者你想在chkFinal驗證完成後做一個ajax調用? –

+0

取決於你想要'打印'做什麼。你想讓它取代整個瀏覽器窗口嗎?你是否想要更新當前窗口中的某個元素?你想讓它提供一個文件下載? –

+0

@Jason你打我:) –

回答

3
$('#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 
    }​ 
}); 
+0

討論[搬到這裏](http://chat.stackoverflow.com/rooms/11975/discussion-between-gdoron-and-pomster) –

+0

@RobertHarvey。感謝評論不結束時我應該怎麼做?在每一個評論中,我確信這將是最後一次,但它沒有... – gdoron

+0

希望大家會看到我的評論和[得到提示。](http://chat.stackoverflow.com/rooms/11975/討論-gdoron間和-波姆斯特爾) –

相關問題