Jadina
Jadina

Reputation: 1

ASP.NET MVC C# - Pass dynamic parameter using URL ACTION

How can I pass the value of my textbox "R_endDate" from my VIEW to my controller ExportToExcel function using Url.Action ?

On my View: Index.cshtml

I have this textbox with value of date.

<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>

I have this anchor tag with url action.

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

On my Controller: MISReportController.cs

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

    }

my scripts

error1

Upvotes: 0

Views: 2456

Answers (2)

Bhinni
Bhinni

Reputation: 31

try using html helper beginform()

in 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>}

in the controller

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

}

Upvotes: 0

jegtugado
jegtugado

Reputation: 5141

You can easily handle this using javascript/jquery.

UI:

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

Script:

<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>

Upvotes: 3

Related Questions