按钮中的Html.ActionLink

Pom*_*ter -5 c# asp.net-mvc jquery razor asp.net-mvc-3

我正在尝试使用按钮连接到控制器中的方法.我可以通过这个链接连接:

@Html.ActionLink("Print", "Print", new { id = Model.SalesContractId})
Run Code Online (Sandbox Code Playgroud)

但我不想要一个链接,我希望我的对话框上的按钮能够做到这一点.我尝试过以下方法:

$('#btnDialogPrint').click(function () {
        location.href = '<%= Url.Action("Print", "Print", new { id = Model.SalesContractId}) %>'; 

    });
Run Code Online (Sandbox Code Playgroud)

但它只是将我重定向到一个说错误请求的页面.

namespace Contract.Controllers
{

    public class ContractController : Controller
    {

        CompassEntities db = new CompassEntities();

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

一旦我进入这个方法,我不会担心里面的代码.

我可以在这附加一个链接吗?

<input type="button" value="Print" id="btnDialogPrint" />
Run Code Online (Sandbox Code Playgroud)

我的视图Edit.cshtml

@model Contract.Models.tbSalesContract
<!DOCTYPE html>
<html>
<head>
    <title>Edit Contract</title>
</head>
<!-- Script section -->
<script src="@Url.Content("~/Scripts/Views/Contract.js")" type="text/javascript"></script>
<!-- Templates -->
<script id="tmplDebtorList" type="text/x-jquery-tmpl">    
    <div class="DebtorResults">
    <span><div style="width:80px;float:left;">${CustomerAccNo}</div></span><span><a href="#" class="debtorLink" value="${DebtorId}">${CustomerName}</a></span>
    </div>
</script>
<body>
    @using (Html.BeginForm("Edit", "Contract", FormMethod.Post, new { id = "frmMain",name="frmMain" }))
    {
Run Code Online (Sandbox Code Playgroud)

mat*_*mmo 9

您在jQuery中的ActionLink链接与链接不匹配.此外,当使用Url.Action时不指定单词Controller,它会把它放在你身上.

编辑:看到您编辑的帖子后,您没有使用正确的控制器.

试试这个:

$('#btnDialogPrint').click(function () {
    location.href = '<%= Url.Action("Print", "Contract", new { id = Model.SalesContractId}) %>'; 
});
Run Code Online (Sandbox Code Playgroud)

Edit2:我知道这个问题!您正在将Razor语法与Web表单语法混合使用!试试这个:

$('#btnDialogPrint').click(function () {
    location.href = '@Url.Action("Print", "Contract", new { id = Model.SalesContractId})'; 
});
Run Code Online (Sandbox Code Playgroud)

另一个问题是你试图在JavaScript文件中执行此操作,你不能在JavaScript文件中使用Razor语法它必须在View中