0

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

2
  • You can better handle anchor click in JavaScript or modify anchor href in javscript code, for example stackoverflow.com/a/12250499/713789 Commented May 15, 2018 at 6:48
  • You cant - @Url.Action() is razor code. It is paersed on the server before the view is sent to the browser. Use a form (with FormMethod.Get and post the form to you method), or use javascript to build the url Commented May 15, 2018 at 6:50

2 Answers 2

3

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>
Sign up to request clarification or add additional context in comments.

15 Comments

ajax call is better than this one?
public FileContentResult ExportToExcel(string param), when I check the param value it is null. Any idea sir?
@MohanSrinivas why is it better? @Jadina check the name of your textbox, I simply assumed it was R_endDate. Change if the razor-generated name is different.
Just pass the model value as a data while click the link and get the result right?
@MohanSrinivas based on my experience with file downloading using ajax, it will not be downloaded as is because your success callback will have to handle blob data. But if you prefer to persue that approach then you may. I am not qualified to decide which is better.
|
0

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){ .......

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.