【问题标题】:Html.ActionLink in a button按钮中的 Html.ActionLink
【发布时间】:2012-05-31 10:06:35
【问题描述】:

我正在尝试使用按钮连接到控制器中的方法。我可以通过此链接进行连接:

@Html.ActionLink("Print", "Print", new { id = Model.SalesContractId})

但我不想要一个链接,我想要我的对话框上的按钮来完成它。我尝试了以下方法:

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

    });

但它只是将我重定向到显示错误请求的页面。

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
        }

不要担心里面的代码,一旦我进入这个方法,我就会得到。

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

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

我的视图 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" }))
    {

【问题讨论】:

  • 我认为您应该在事件处理程序中进行 AJAX 获取!
  • $.ajax({ url: '@Url.Action("Print","Contract", new {id = Model.SalesContractId})' });这不起作用以太
  • @daveL 你能看看我的 ajax 事件并告诉我我做错了什么
  • 你忽略了返回结果..看看api.jquery.com/jQuery.get

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


【解决方案1】:

您在 jQuery 中的链接与 ActionLink 链接不匹配。另外,在使用Url.Action 时不要指定单词Controller,它会为您输入。

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

试试这个:

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

Edit2:我知道问题所在!您将 Razor 语法与 Web 表单语法混合在一起!试试这个:

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

另一个问题是您尝试在 JavaScript 文件中执行此操作,您不能在必须位于视图中的 JavaScript 文件中使用 Razor 语法

【讨论】:

    【解决方案2】:

    博斯特;你把 Razor 和 aspx 混在一起了。。

    使用 @Url.Action("Print", "Contract", new { id = Model.SalesContractId}) 创建您的链接,即

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

    你在其他地方做过这个吗?

    您的控制器也是合同而不是打印

    【讨论】:

    • 它说,路径中有非法字符。
    • 你把我弄糊涂了……这个页面是由razor引擎还是aspx引擎渲染的?
    【解决方案3】:

    您在一个代码 sn-p 中使用 Razor,在下一个代码中使用 asp。这是故意的吗?

    假设您只是想调用控制器:

    首先尝试在您的代码中更改以下内容:

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

    <%=Html.ActionLink("Print", "Contract", new { id = Model.SalesContractId})%>
    

    否则:

    使用 Ajax:

    在正文中:

    <input type="button" value="Print" id="btnDialogPrint" onclick="print()"/>
    

    在脚本中:

    <script type="text/javascript">
        function print() {
                $.get('@Url.Action("Print", "Contract", new { id = Model.SalesContractId})');
            }
    </script>
    

    【讨论】:

    • 只需添加
      对不起,我还在写这篇文章。它将在几分钟内更新。
    • 只需检查你 " 和 ' ,我是 js 新手,所以不会知道错误,但我可以看到你结束了你
    • 我的错。 onclick="print()' 应该是 onclick="print()"。
    • +1 好的,您的代码将我带到打印屏幕,谢谢,但您将我带到最后并跳过了中间我的打印方法将在我的控制器中。
    【解决方案4】:

    这是我一直在寻找的,但谢谢大家。

    $('#btnDialogPrint').click(function () {
           window.location = '../Print/' + $('#SalesContractId').val();
        });
    

    【讨论】:

    • 那你需要把这个设置为答案。
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签