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

Jadina
  • 1
  • 3
  • You can better handle anchor click in JavaScript or modify anchor href in javscript code, for example https://stackoverflow.com/a/12250499/713789 – Anirudha Gupta May 15 '18 at 06: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 –  May 15 '18 at 06:50

2 Answers2

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>
jegtugado
  • 5,081
  • 1
  • 12
  • 35
  • ajax call is better than this one? – Mohan Srinivas May 15 '18 at 06:56
  • public FileContentResult ExportToExcel(string param), when I check the param value it is null. Any idea sir? – Jadina May 15 '18 at 07:07
  • @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. – jegtugado May 15 '18 at 07:09
  • Just pass the model value as a data while click the link and get the result right? – Mohan Srinivas May 15 '18 at 07:13
  • @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. – jegtugado May 15 '18 at 07:18
  • -the name of my textbox is R_endDate base on the above code. i.e. "@Html.TextBoxFor(m => m.R_endDate..." I'm using alert too, but it is not alerting the param. $("#aExportToExcel").onclick(function () { var param = $("input[name='R_endDate']").val(); alert(param); <-- not alerting – Jadina May 15 '18 at 07:19
  • @Jadina try changing the a href to "#" to prevent the default behavior of anchor elements. Sorry I forgot to write it that way. – jegtugado May 15 '18 at 07:27
  • @JohnEphraimTugado, when i changed it, it is not clickable anymore... it is not going to my controller function when debugging. – Jadina May 15 '18 at 07:38
  • @Jadina change `onclick` to `click`. I got mixed up with jquery and basic javascript. No excuse. – jegtugado May 15 '18 at 07:44
  • @JohnEphraimTugado still the same sir... , I've added alert below document.ready function but it is not alerting "$(document).ready(function () { alert('test');", Is there a problem with the script? – Jadina May 15 '18 at 07:50
  • @Jadina where are you placing your scripts? In MVC I believe you should place it inside `@section scripts {}`. See this [answer](https://stackoverflow.com/a/23327673/6138713) for more info. – jegtugado May 15 '18 at 07:56
  • yes sir, i included that on my section scripts see my attached image on my post... here's the link [link]https://i.stack.imgur.com/xbSVY.png – Jadina May 15 '18 at 08:07
  • @Jadina you need `jquery` for this. Is it included in your `_Layout.cshtml`? You can try pressing `F12` in your browser and on the console tab see if there are any errors which you have to address. Do this before refreshing the page. – jegtugado May 15 '18 at 08:27
  • Sir, please check the error now, here's the link... Yes, it's passing the parameter but it is not going to my function when debugging... https://i.stack.imgur.com/yYMbX.png – Jadina May 16 '18 at 00:18
  • @Jadina your razor code `@Url.Action` did not work. You must make sure to place it on the cshtml page so that the razor syntax could be recognized. – jegtugado May 16 '18 at 06:56
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){ .......

}
Bhinni
  • 31
  • 5