0

我想在@Url.Action 中传递两个参数。

看法:

 <select id="ddlSearchBy" name="ddlSearchBy" style="width: 150px">
      <option value="TaxCode">Tax Code</option>
      <option value="TaxDescription">Tax Description</option>
      <option value="ClassDescription">Class Description</option>
      <option value="ZoneName">Zone Name</option>
  </select>
  <input type="text" name="txtSearchValue" id="txtSearchValue"/>
  <button type="button" id="btnDownload">Download</button>
  <button type="button" id="btnSearch">Search</button>

单击下载按钮时,我在 Masters Controller 中调用方法“ExportToExcel”,还需要传递两个参数。即选择html和文本框值的选定值。

现在,我正在使用如下;

  <button type="button" id="btnDownload" onclick="location.href='@Url.Action("ExportToExcel", "Masters", new { ddlSearchBy = @TempData["ddlSearchBy"], txtSearchValue = @TempData["txtSearchValue"] })'">Download</button>

我可以直接在@Url.Action 中传递html 控件值吗?

编辑:

控制器:

[HttpPost]
public ActionResult ExportToExcel(string ddlSearchBy, string txtSearchValue)
    {
        var grid = new GridView();

        grid.DataSource = from data in GetTaxMasterTable(ddlSearchBy, txtSearchValue)
                          select new
                              {
                                  Code = data.taxCode,
                                  TaxDescription = data.taxDescription,
                                  ClassDescription = data.classDescription,
                                  Location = data.locationShortName,
                                  Zone = data.zoneName,
                                  TaxValue = data.taxValue,
                                  Tax = data.taxPercentage,
                                  ModifiedDate = data.modifiedDate
                              };

        grid.DataBind();

        Response.ClearContent();
        Response.AddHeader("Content-Disposition", "attachment; filename = TaxMaster.xls");
        Response.ContentType = "application/vnd.ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);

        grid.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();

        return RedirectToAction("TaxMaster");           
    }

jQuery:

$("#btnSubmitDownloadExcel").click(function (e) {
            var ddlSearchBy = $("#ddlSearchBy").val();
            var txtSearchValue = $("#txtSearchValue").val();
            $.ajax({
                type: "POST",
                url: "/Masters/ExportToExcel",
                data: JSON.stringify({ "ddlSearchBy": ddlSearchBy, "txtSearchValue": txtSearchValue }),
                contentType: "application/json; charset=utf-8",
                datatype: "json",
                success: function (data) {
                    alert(JSON.stringify(data));                        
                },
                error: function (data) {
                    alert("err");
                    alert(JSON.stringify(data));
                }
            });
        });

此代码不下载 Excel。

4

1 回答 1

1

您不能使用常规超链接 (Url.Action) 从输入元素传递值,而无需借助 javascript。

但是您可以简单地使用带有 GET 方法的表单。

<form action="Masters/ExportToExcel" method="get">
  <select id="ddlSearchBy" name="ddlSearchBy" style="width: 150px">
      <option value="TaxCode">Tax Code</option>
      <option value="TaxDescription">Tax Description</option>
      <option value="ClassDescription">Class Description</option>
      <option value="ZoneName">Zone Name</option>
  </select>
  <input type="text" name="txtSearchValue" id="txtSearchValue"/>
  <button type="submit" id="btnDownload">Download</button>
</form>

可能希望使用 Html.BeginForm() 帮助器生成表单以获得更简洁的代码,但最终结果是相同的。

更新 - 如果您已经有一个带有另一个提交操作的表单

如果您不需要支持 IE 9 或更低版本,您可以使用该formaction属性让按钮更改表单的操作。

例子:

<form action="SomeController/Search" method="get">
      <select id="ddlSearchBy" name="ddlSearchBy" style="width: 150px">
          <option value="TaxCode">Tax Code</option>
          <option value="TaxDescription">Tax Description</option>
          <option value="ClassDescription">Class Description</option>
          <option value="ZoneName">Zone Name</option>
      </select>
      <input type="text" name="txtSearchValue" id="txtSearchValue"/>
      <button type="submit" id="btnSearch">Search</button>
      <button type="submit" id="btnDownload" formaction="Masters/ExportToExcel">Download</button>
    </form>

在本例中,当您单击 btnSearch 按钮时,表单将默认在 SomeController/Search 上执行获取操作。但是,如果您单击 btnDownload 按钮,表单将向 Masters/ExportToExcel 发出获取请求。

如果需要支持 10 以下版本的 IE,则需要在提交之前使用 javascript 更改表单的操作。

使用 jQuery 的示例:

<form action="SomeController/Search" method="get">
          <select id="ddlSearchBy" name="ddlSearchBy" style="width: 150px">
              <option value="TaxCode">Tax Code</option>
              <option value="TaxDescription">Tax Description</option>
              <option value="ClassDescription">Class Description</option>
              <option value="ZoneName">Zone Name</option>
          </select>
          <input type="text" name="txtSearchValue" id="txtSearchValue"/>
          <button type="submit" id="btnSearch" onclick="$(this).closest('form').attr('action','SomeController/Search');">Search</button>
          <button type="submit" id="btnDownload" onclick="$(this).closest('form').attr('action','Masters/ExportToExcel');">Download</button>
        </form>
于 2015-11-04T10:39:47.587 回答