我是C#的新手,我想知道是否可以使用@ Html.ActionLink命令在同一视图中执行代码块?
例如:
//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>
理想情况下,会在页面加载(社区)上进行默认选择,然后当用户单击社区或运行状况按钮(引导选项卡样式)时,相应的代码块将运行。
不确定使用Actionlink命令是否可行,或者可能有更好/更简单/更简单的方法来完成这些任务。
更新
在下面尝试了一个解决方案,并且没有任何快乐,所以我要包含所有代码以帮助更详细地解释。
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
使用Sitecore.Data.Items; 使用System.Collections.Generic;
namespace iehp.EventCalendar.Models
{
public class EventViewModel
{
public Item Item { get; set; }
public List<Item> Children { get; set; }
}
}
EventCalendarCtrl(部分视图)
@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(视图)
@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>
谢谢!
答案 0 :(得分:0)
使用局部视图将是这样的
_CommunPage.cshtml:
@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="@(ViewBag.Tab == "Community" ? "nav-item active" : "nav-item")">
@Html.ActionLink("Community", "", new { id = "Community" }, new { @class = "nav-link")
</li>
<li class="@(ViewBag.Tab == "Health" ? "nav-item active" : "nav-item")">
@Html.ActionLink("Health", "", new { id = "Health" }, new { @class = "nav-link")
</li>
</ul>
</div>
Community.cshtml:
@{
ViewBag.Title = "Community Page Tile";
ViewBag.Tab = "Community";
}
@Html.Partial("_CommunPage")
Html for the Community page....
Health.cshml:
@{
ViewBag.Title = "Health Page Tile";
ViewBag.Tab = "Health";
}
@Html.Partial("_CommunPage")
Html for the Health page....