ASP.NET MVC C# - 使用URL ACTION传递动态参数

时间:2018-05-15 06:45:35

标签: c# asp.net-mvc

如何使用Url.Action将我的文本框“R_endDate”的值从我的VIEW传递到我的控制器ExportToExcel函数?

在我的视图:Index.cshtml

我的文本框中包含日期值。

<div class="uk-form-row">
    <label class="uk-form-label" for="R_endDate">Ending Date:</label>
    <div class="uk-form-controls">
        @Html.TextBoxFor(m => m.R_endDate, new { @class = "uk-form-small uk-text-left required", @maxlength = 12, @data_uk_datepicker = "{format: 'MM/DD/YYYY'}" })

    </div>
</div>

我有这个带有网址操作的锚标记。

<a href="@Url.Action("ExportToExcel")" class="uk-button uk-button-small uk-text-center uk-text-small ">Export</a>

在我的控制器上:MISReportController.cs

 public FileContentResult ExportToExcel()
    {
    byte[] filecontent = ExcelExportHelper.ExportExcel(list, "Technology", true, strAddtional);
    return File(filecontent, ExcelExportHelper.ExcelContentType, strTitle);

    }

my scripts

error1

2 个答案:

答案 0 :(得分:3)

您可以使用javascript / jquery轻松处理此问题。

UI:

<a id="aExportToExcel" href="#" class="uk-button uk-button-small uk-text-center uk-text-small ">Export</a>

脚本:

<script type="text/javascript">

$(document).ready(function () {
    $("#aExportToExcel").click(function () {
        var param = $("input[name='R_endDate']").val();
        var _url = '@Url.Action("ExportToExcel", "MISReportController", new { param = "XXX" })'; // Param is the example parameter name. Change as needed.
        _url = _url.replace("XXX", param); // _url will contain your url string so you can just play with it as needed for your requirement.

        window.open(_url, "_blank");
    });
});

</script>

答案 1 :(得分:0)

尝试使用html helper beginform()

index.cshtml中的

   @using (Html.BeginForm("ExportToExcel", "MISReport")) {<div class="uk-form-row">
<label class="uk-form-label" for="R_endDate">Ending Date:</label>
<div class="uk-form-controls">
    @Html.TextBoxFor(m => m.R_endDate, new { @class = "uk-form-small uk-text-left required", @maxlength = 12, @data_uk_datepicker = "{format: 'MM/DD/YYYY'},@name="date" })</div></div>}
控制器中的

      [HttpPost]public FileContentResult ExportToExcel(Datetime date){ .......

}