Ari
Ari

Reputation: 271

Can i pass a Model from view to controller without Form?

Hi guys i have difficulties of passing my model to controller> I wonder if this is a good practice or is it possible to do so. Here's what i want to achieve.

<% foreach (DownloadFile file in Model){ %>
     <a href="<%= Url.Action("DownloadFile", new { File  = file}) %>">click here to download</a>
 <% } >%

I want to pass the DownloadFile Object "file" to my controller that goes like this:

public ActionResult DownloadLabTestResult(DownloadFile File)
    {
        DownloadFile file = File;

        ...
        return new FileStreamResult(Response.OutputStream, Response.ContentType);

    }

I tried passing a string or integer and its doable. but when i want to pass an object like the above, i get a null value. What's the proper way to do this? thank u!

Upvotes: 2

Views: 418

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062745

When using FileStreamResult, you need to give it the stream that represents the contents of the file, which will be consumed and sent to the client. Currently you've instead given it the ASP.NET response stream instead. It can't possibly read from that (it is an output-only stream).

So; where is the contents? Open up a stream to that and pass it in. Depending on your implementation, this could mean the local file-system, a network file-system, a database, a remote http (etc) server, or something generated in-memory (typically via MemoryStream).

Equally, it is for you to tell MVC what the content-type is; you shouldn't use the value from Response.*, since that is what you are constructing.

Upvotes: 1

Related Questions