Qwerty
Qwerty

Reputation: 539

Hide items according to date difference

I have a list of files which opens via modal. What I want is that it should hide the file 30 days after the file was generated.

Here's the code that displays the files

 <table>
                @foreach (FileInfo res in Model.PDFFile)
                {
                    <tr>
                        <td>@res.Name.Splitter('_', 1)</td>
                        <td>
                            <a data-toggle="modal" href="#testmodal@(res.Name.Splitter('_', 0))">View Result</a>
                            <div class="modal fade" id="testmodal@(res.Name.Splitter('_', 0))" role="dialog" tabindex="-1">
                                <div class="modal-dialog modal-lg">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <button type="button" class="close" data-dismiss="modal" data-toggle="tooltip" title="Close"><span class="glyphicon glyphicon-remove"></span></button>
                                        </div>
                                        <div class="modal-body">
                                            <embed src="~/Files/@res.Name" width="850" height="1000" type="application/pdf" />
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </td>
                    </tr>
                }
    </table>

And here's the controller :

 public ActionResult Index()
    {
        ResultModel rmodel = new ResultModel();
        string path = Server.MapPath("~/Files/");
        DirectoryInfo dir = new DirectoryInfo(path);
        rmodel.PDFFile = dir.GetFiles("*.pdf*");
        return View(rmodel);

    }

The filename includes the date of the file. Do you have any idea how to do this in javascript ? Thanks !

Upvotes: 0

Views: 64

Answers (1)

user3559349
user3559349

Reputation:

There is no point sending all the files to the view and then hiding them in the view (you might be generating html for a hundreds of files but only showinh a few) and you should just filter them in the controller. In addition, its not clear why you would need to include the date in the file name itself, as FileInfo contains a DateTime CreationTime property that you can use for filtering based on the date the file was uploaded.

To filter the files in the controller

public ActionResult Index()
{
    DateTime minDate = DateTime.Today.AddDays(-30);
    string path = Server.MapPath("~/Files/");
    DirectoryInfo dir = new DirectoryInfo(path);
    ResultModel rmodel = new ResultModel()
    {
        PDFFile = dir.GetFiles("*.pdf*").Where(x => x.CreationTime > minDate);
    };
    return View(rmodel);
}

And assuming you remove the date from the filename, then you can just pass a collection of file names to the view rather that a collection of FileInfo, for example

ResultModel rmodel = new ResultModel()
{
    PDFFile = dir.GetFiles("*.pdf*")
       .Where(x => x.CreationTime > minDate).Select(x => x.Name);
};

where PDFFile is IEnumerable<string> rather that IEnumerable<FileInfo> (although its not clear what your Splitter() extension method is actually doing)

Upvotes: 1

Related Questions