0

我有一个链接女巫我想你变成一个按钮,链接通过一个参数,看起来如下:

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

我现在想用这个参数{ id = item.SalesContractId }的按钮替换它

我的按钮如下所示:

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

有人可以告诉我我该怎么做吗?

这是我的页面的外观,因此您可以看到我想要实现的目标:

@model IEnumerable<Contract.Models.tbSalesContract>


<!DOCTYPE html>

<html>
<head>
    <title>Index</title>
</head>
<body>
    <p>
        @Html.ActionLink("Create New", "Edit")
    </p>
    <table>
        <tr>            
            <th>
                Company
            </th>
            <th>
                Trading As
            </th> 
            <th>
                Created
            </th>           
            <th>
                Updated
            </th>
            <th></th>
        </tr>

    @foreach (var item in Model) {
        <tr>                     
            <td>
                @Html.DisplayFor(modelItem => item.CompanyName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.TradingAs)
            </td> 
            <td>
                @Html.DisplayFor(modelItem => item.C_date)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.C_updateDate)
            </td>
            <td>
               @* @Html.ActionLink("View", "Edit", new { id = item.SalesContractId }) *@
                <input type="button" value="View" id="btnFromIndexView" />

            <td>
            @* Add Button to print only if contract state is finalized *@
            @if (item.IsFinal) 
            {
                @Html.ActionLink("Print", "Print", new { id = item.SalesContractId })
                <input type="button" value="Print" id="btnFromIndexPrint" />

            }
            </td>
            </td>
        </tr>
    }

    </table>
</body>
</html>
4

2 回答 2

2

如果您查看 ActionLink 生成的源代码,您会发现这只是一个常规超链接。id 参数附加到您的 URL。

所以,用按钮替换你的超链接就不一样了。

在 ASP.NET MVC 中,数据可以通过多种方式发送到您的服务器。您的 ActionLink 使用的是将您的数据附加到 URL。路由机制会将这些映射到您的 Action 方法上的特定属性。

另一种发送数据的方法是使用 HTML 表单。表单可以有一个提交按钮,然后将表单数据发送到您的服务器。

为您的操作方法搜索可能的值时,ASP.NET MVC 将检查以下来源:

  1. 之前绑定的动作参数,当动作是子动作时
  2. 表单域 (Request.Form)
  3. JSON 请求正文 (Request.InputStream) 中的属性值,但仅当请求是 AJAX 请求时
  4. 路线数据 (RouteData.Values)
  5. 查询字符串参数 (Request.QueryString)
  6. 发布的文件 (Request.Files)
于 2012-06-04T12:45:47.107 回答
1

为什么要使用按钮而不是链接?如果您担心外观,那么您可以应用一些 CSS 并使链接看起来像按钮。

超链接(<a>)是为在网页之间形成连接而创建的。我建议您在您的情况下使用超链接而不是按钮。如果您想使用 javascript 执行一些客户端操作,按钮(除了提交)是很好的。

如果您仍然想使用按钮,那么您必须依靠一些 javascript 来调用控制器操作。

于 2012-06-04T13:53:01.883 回答