Reputation: 147
I actually have a two part question here. I am building a helper to control when certain content is rendered to the public. I have a customer website who needs content to go live at certain times. So for example a new hero image on the home page needs to go live at 1AM on the 12th and needs to stop being rendered on the 19th at 1AM. I need to be able to use this helper in my view like so:
@Html.DurationControl("Partial/_HeroImage", new DateTime(2015, 02, 12, 01, 00, 00), new DateTime(2015, 02, 19, 01, 00, 00))
Is it possible to return a partial view from an HTML.Helper?
Is this approach the best way of accomplishing my goal?
public static PartialViewResult DurationControl(this HtmlHelper html, string view, DateTime start, DateTime end){
DateTime Now = DateTime.Now;
if (Now > start && Now < end)
{
return PartialView(view);
}
}
Note: Thanks to the answers below from Berin Loritsch and Chris Pratt, I was able to get this to work. Here is what I ended up with:
public static MvcHtmlString DurationControl(this HtmlHelper html, string view, DateTime start, DateTime end)
{
DateTime Now = DateTime.Now;
if (Now > start && Now < end)
{
return html.Partial(view);
}
else
{
return null;
}
}
Upvotes: 3
Views: 2024
Reputation: 11463
In Asp.Net MVC you have two concepts you want to keep straight:
It's unclear which you are trying to control based on time, so I'll cover both cases.
Choosing one page vs. another
You receive a request in your controller, and you choose which view to return. In this case your code is pretty simple:
[Post]
public ActionResult Page(string view, DateTime start, DateTime end)
{
// We are handling user input from a form
DateTime Now = DateTime.Now;
if (Now > start && Now < end)
{
return View(view);
}
return View();
}
In all likelihood you would be pulling the view and the start and end dates from your own code, possibly even the session information. The effect of this approach is to return one page vs. another.
Embedding a snippet
This is where HTML helpers come in to play. Partials are typically named with a leading underscore "_Partial.cshtml". The point of HTML helpers is to write to the output stream as it's processed. In essence, it's inserting HTML for you. In that case, you would change your code a little bit:
public static ActionResult DurationControl(this HtmlHelper html,
string view, DateTime start, DateTime end)
{
DateTime Now = DateTime.Now;
if (Now > start && Now < end)
{
RenderPartial(view);
}
}
MVC will look in the current controller's views first, then fall back to the shared views folder if it doesn't find it there.
Upvotes: 1
Reputation: 239260
If you just put this right into your view, this is the code you'd have:
@if (DateTime.Now > new DateTime(2015, 02, 12, 01, 00, 00) && DateTime.Now < new DateTime(2015, 02, 19, 01, 00, 00))
{
@Html.Partial("partial/heroimage")
}
Granted, it's not the prettiest thing ever, but it's also not that bad. An argument could be made that this is perfectly acceptable. If you really need to abstract this rather basic logic away, you just need to model your helper to work as this code would:
public static MvcHtmlString DurationControl(this HtmlHelper helper, string partial, DateTime start, DateTime end)
{
if (DateTime.Now > start && DateTime.Now < end)
{
return helper.Partial(partial);
}
return new MvcHtmlString();
}
Upvotes: 2
Reputation: 3597
No. Views should be returned by your controllers, so you can put your logic there. HTML Helpers can help you to build some custom stuff from within your views (in most cases, however, PartialViews would be a better fit here, too).
Upvotes: 0