This is the closest thing I could find to this specific answer, but doesn't quite answer it for me. How can I create a Html Helper like Html.BeginForm

I have a current html.helper that hides (or shows) content based on the current server time. It's not really a problem, but an improvement I'd love to see here. So the HTML helper works like this:

Html.DurationHtml works like this:

public static IHtmlString DurationHtml(this HtmlHelper html, string StartDateTime, string EndDateTime)
    {
        if (CentralLibrary.WithinDatespan(StartDateTime, EndDateTime))
        {
            var writer = html.ViewContext.Writer;
            return html.Raw(htmlContent);
        }
        else
        {
            return null;
        }
    }

CentralLibrary.WithinDatespan(start,end) is just a method that compares the current time (or a provided simulated time) with the date/times specified and returns true or false.

I implement it like so:

@Html.DurationHtml("<a href=\"link\">Content</a>", "12/12/2015 12:00:01pm","12/12/2017 12:00:01pm") 

This works and does exactly what I want it to do, very well. My hope for an improvement here is to take the content out of the double quotes so that I don't have to escape the double quotes. The caveat here is that I cannot have the content sent to the browser at all. I know I could wrap a container with display set to none, but the content cannot be sent at all as some of the info may not be for public eyes until certain times. Is there a way I can make the html helper work like this and keep the content from being sent to the browser?

@using(Html.DurationHtml("12/12/2015 12:00:01pm","12/12/2017 12:00:01pm")){

     <a href="link">Content</a>

}

Related posts

Recent Viewed