Reputation: 3496
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
}
Upvotes: 0
Views: 648
Reputation: 218732
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.
Upvotes: 2
Reputation: 5943
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.
Upvotes: 1