views:

38

answers:

1

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!

+1  A: 

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.

Marc Gravell