0

I'm trying to download a file with an html.actionlink(text, action, controller, reoute values, htmlattributes)

I'm trying to pass the view model and text from an <input type="text" to the controller. But I'm having trouble passing the input text to the MVC controller. I'm using an actionlink because I can't seem to download the file with post

How can I pass the Model values and the text input to the controller? This is the HTML

<body>
    <div class="col-md-12 form-group centered">
        <div class="col-md-4"></div>
            <div class="col-md-4">
                <label for="quoted">Quoted:</label>
                <input type="text" class="form-control" style="width:300px" id="quoted">
            </div>
        <div class="col-md-4"></div>

        </div>
    <div class="col-md-12 text-center">
        @Html.ActionLink("Download", "GeneratePoExportDocument", "HealthCareExport", new {model = Model, quoted = "text box input goes here" }, new { @class = "btn btn-default", id="download-btn" })
    </div>

Are there other methods to download a file where I can pass in all the values?
</body>

Here is the controller

public FileResult GeneratePoExportDocument(MyModel model, string quoted)
{
  //my model has all of the values
  //quoted is null I don't know how to pass that in
}
Keyur Ramoliya
  • 1,900
  • 2
  • 16
  • 17
Train
  • 3,420
  • 2
  • 29
  • 59
  • You cannot submit values to a controller from a view via actionlink.. they have to be submitted via form elements. Furthermore, `string quoted` will never receive a value because the input doesn't have the corresponding `name` attribute. – Grizzly Jun 22 '18 at 16:15
  • @M12Bennett Isn't a form `POST`? Is it possible to download a file that way? – Train Jun 22 '18 at 16:28
  • I think I'll try something like this https://stackoverflow.com/questions/16670209/download-excel-file-via-ajax-mvc it might be useful. – Train Jun 22 '18 at 16:32
  • I apologize, I read the question wrong, so you just want to send the model and the string to an action to download the file.. so is `model` passing through correctly? – Grizzly Jun 22 '18 at 16:32
  • @M12Bennett yes, however I'm having trouble passing the textbox input value. – Train Jun 22 '18 at 16:38

2 Answers2

2

You cannot, and should not pass your entire model like that in the querystring. The textbox in part of the rendered HTML and user can enter any value there. So if you want to send that data, either you have to hijack the click event of your link using javascript, read the input element value and append that to the querystring and navigate to that url by setting window.location.href value.

Another (more solid IMHO) option is to do a form submit. If you want to send certain properties of your view model, you may include them as hidden input in the form

<form action="@Url.Action("GeneratePoExportDocument","HealthCareExport")" method="post">

    @Html.HiddenFor(a => a.Id)
    @Html.HiddenFor(a => a.CartId)

    <label for="quoted">Quoted:</label>
    <input type="text" class="form-control" style="width:300px" name="quoted">
    <button type="submit">Download</button>
</form>

Send only the proeprties you absolutely want in the action method. If all you need is theId, send only that. Perhaps you can rebuild your entire model in that action method from the Id if needed.

Shyju
  • 214,206
  • 104
  • 411
  • 497
1

Try doing this with Javascript:

Replace your ActionLink with this:

<a href="@Url.Action('GeneratePoExportDocument', 'HealthCareExport', new {model = Model, quoted = 'placeholder'})" id='myLink'>Download</a>

Then use blur function in javascript for the textbox.

$("#quoted").blur(function() {
    var currentUri = $("#myLink").attr("href");
    var newUri = currentUri.replace("placeholder", $(this).val());
    $("#myLink").attr('href', newUri);
});

Let me know if this helps.

Grizzly
  • 5,873
  • 8
  • 56
  • 109