I'm new to C#, and I'm wondering if it's possible to use an @Html.ActionLink command to execute a block of code in the same view?
So for example:
//bootstrap tab button 1
<@Html.ActionLink("Community", "", new {id="Community"}, new {@class= "nav-link active" }, new {data-toggle="tab"})
//bootstrap tab button 2
<@Html.ActionLink("Health", "", new {id="Health"}, new {@class= "nav-link" }, new {data-toggle="tab"})
<div id="Community"> //some code here </div>
<div id="Health"? //some code here </div>
Ideally there would be a default choice made on page load (community), and then when the user clicked on either the community, or health buttons(bootstrap tabs style) the corresponding code block would get run.
Not sure if this is possible with the Actionlink command, or perhaps there's a better/easier/simpler way to accomplish these tasks.
Update:
Attempted a solution below, and had no joy, so am including all of my code to help explain in greater detail.
EventCalendarController.cs
using iehp.EventCalendar.Models;
using Sitecore;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Mvc.Controllers;
using System.Linq;
using System.Web.Mvc;
namespace iehp.Controllers
{
public class EventCalendarController : SitecoreController
{
public ViewResult EventCalendarCtrl()
{
//read in config file & get parentGuid value
var parentGuidValue = Sitecore.Configuration.Settings.GetSetting("eventCalendarFolderGuid");
//init Sitecore db
Database database = Context.Database;
var model = new EventViewModel();
model.Item = database.GetItem(parentGuidValue);
model.Children = model.Item.Children.ToList();
return View("/Views/Components/EventCalendarCtrl.cshtml", model);
}
}
}
EventCalendarCtrlModel.cs
using Sitecore.Data.Items; using System.Collections.Generic;
namespace iehp.EventCalendar.Models
{
public class EventViewModel
{
public Item Item { get; set; }
public List<Item> Children { get; set; }
}
}
EventCalendarCtrl(partial view)
@model iehp.EventCalendar.Models.EventViewModel
<div class="col-12"><h4>Upcoming Events</h4></div>
<div class="col-12 text-right"><a href="#" class="moreEvents">load more >> </a></div>
<div class="col-24">
<ul id="eventsTab" class="nav nav-tabs nav-fill">
<li class="nav-item">
@Html.ActionLink("Community", "", new { id = "Community" }, new { @class = "nav-link" + @ViewBag.Tab == "Community" ? " active" : "" })
</li>
<li class="nav-item">
@Html.ActionLink("Health", "", new { id = "Health" }, new { @class = "nav-link" + @ViewBag.Tab == "Health" ? " active" : "" })
</li>
</ul>
</div>
Community.cshtml(view)
@using Sitecore.Data.Fields
@using Sitecore.Data.Items
@using Sitecore.Mvc
@using Sitecore.Resources.Media
@model iehp.EventCalendar.Models.EventViewModel
@{
ViewBag.Tab = "Community";
}
@Html.Partial("EventCalendarCtrl")
<div id="Community" class="card-deck">
@foreach (var child in Model.Children)
{
Item item = Sitecore.Context.Item;
ImageField imgField = ((ImageField)child.Fields["Image"]);
<div class="card" style="margin-right:20px;">
<img class="card-img-top" src="@MediaManager.GetMediaUrl(imgField.MediaItem)" />
<div class="card-body">
<p class="card-text">@Html.Sitecore().Field("Description", child)</p>
<p class="date-time"><strong>Event Date:</strong> @Html.Sitecore().Field("Event Date", child)</p>
</div>
</div>
}
</div>
thanks!