在开始之前,我对编码还比较陌生,并且由于即将在未来开始担任我的第一个初级职位,所以我目前的技能水平非常基础。
我正在创建一个个人C#ASP.NET MVC Web应用程序,以跟踪用户参加的音乐事件,在这些事件中播放的艺术家以及用户看到的艺术家。这一切都由用户手动完成,它从数据库(SQL Server)中所有事件的索引视图中手动添加事件,如下面链接的图像所示。
https://i.stack.imgur.com/HOUGG.png
控制器动作:
public ActionResult GetEvents()
{
return View(_trackerService.GetEvents());
}
视图的标记:
@model IEnumerable<Tracker.Data.tbl_events>
@{
ViewBag.Title = "GetEvents";
}
<h2>GetEvents</h2>
<p>
@Html.ActionLink("Create New", "CreateEvent")
</p>
<table class="table">
<tr><h2>Upcoming Events</h2>
<th>
@Html.DisplayNameFor(model => model.Event_ID)
</th><th>
@Html.DisplayNameFor(model => model.Event_Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Event_Date)
</th>
<th>
@Html.DisplayNameFor(model => model.Event_Location)
</th>
</tr>
@foreach (var item in Model.Where( x => x.Event_Date >= System.DateTime.Now).OrderBy(x => x.Event_Date))
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Event_ID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Event_Name)
</td>
<td>
@Convert.ToString(string.Format("{0:dd/MM/yyyy}", item.Event_Date)) @*Converts the DateTime data type that ASP.NET uses by default into a string with the format of Date Only (https://stackoverflow.com/a/34990313/12764653)*@
</td>
<td>
@Html.DisplayFor(modelItem => item.Event_Location)
</td>
<td>
@*@Html.ActionLink("Edit", "Edit", new { id = item.Event_ID }) | *@
@Html.ActionLink("Details", "GetEventDetails", new { Event_ID = item.Event_ID }) |
@Html.ActionLink("Lineup", "../Event/GetLineup", new { Event_ID = item.Event_ID, Event_Name = item.Event_Name }) |
@if ((System.Web.HttpContext.Current.User != null) && System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{@*Checks to see if there is a current user*@
@Html.ActionLink("Add to my Events", "../Event/AddToUser", new { User_ID = Convert.ToString(System.Web.HttpContext.Current.User.Identity.GetUserId()).GetHashCode(), Event_ID = item.Event_ID })}
@*@Html.ActionLink("Delete", "Delete", new { id = item.Event_ID })*@
</td>
</tr>
}
</table>
用户将事件添加到他们的个人资料后,它会使用以下字段在数据库中创建一个条目(链接如下):
https://i.stack.imgur.com/YQqHT.png
“添加到我的事件”功能的控制器方法:
[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
public ActionResult AddToUser(tbl_eventhistory _event)
{
try
{
_trackerService.AddToUser(_event);
return RedirectToAction("GetEvents");
}
catch
{
return View();
}
}
我了解,就目前的情况而言,很可能必须在控制器或视图中完成此操作,并检查“ tbl_eventhistory”以查看当前用户User_ID和特定Event_ID是否存在条目,但是我不确定如何实际执行此操作,因此将不胜感激。
编辑:显示的表是'tbl_events'的索引视图。当用户从该视图向其个人资料添加事件时,它将使用该特定事件的参数在另一个名为“ tbl_eventhistory”的表中创建一个条目,该参数通过Event_ID(tbl_event的PK)上的外键关联。当用户将事件添加到tbl_eventhistory时,我只想从该特定事件的视图中删除“添加到用户”链接。
设法终于找到了解决方案。如果术语不正确,请原谅我。
我已经有一个函数来返回特定用户事件的索引视图(下面的DAO类代码)
public IList<tbl_eventhistory> GetUserEvents(string User_ID)
{
IQueryable<tbl_eventhistory> _eventHistory;
_eventHistory = from tbl_eventhistory in _context.tbl_eventhistory where tbl_eventhistory.User_ID == User_ID select tbl_eventhistory;
return _eventHistory.ToList<tbl_eventhistory>();
}
考虑如何在具有不同绑定模型的视图中使用此数据,我想到了使用ViewBag并遇到以下问题:“如何检查ViewBag中的列表是否包含字符串”。从这个答案中,我将“ GetEvents”功能修改为以下内容:
public ActionResult GetEvents()
{
List<string> UsersEvents = new List<string>();
foreach (var item in _trackerService.GetUserEvents(User_ID))
{
UsersEvents.Add(item.Event_ID.ToString());
}
ViewBag.MyEvents = _trackerService.GetUserEvents(User_ID);
ViewBag.MyEvents = UsersEvents;
return View(_trackerService.GetEvents());
}
如果我的理解是正确的,这将创建一个List,该列表通过调用'GetUserEvents'操作填充,并循环遍历结果中的每个条目。然后将此列表移到ViewBag上,使我可以修改“ GetEvents” ActionLink,以在现有语句中包含一个附加的IF语句,以检查用户是否已登录:
@if ((System.Web.HttpContext.Current.User != null) && System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
if (((IList<string>)ViewBag.MyEvents).Contains(item.Event_ID.ToString())){
<p>TEST</p> }
else{
@Html.ActionLink("Add to my Events", "../Event/AddToUser", new { User_ID = Convert.ToString(System.Web.HttpContext.Current.User.Identity.GetUserId()).GetHashCode(), Event_ID = item.Event_ID })}
}
这导致显示“测试”,而不是在适当的地方显示“添加到用户”的链接(如下所示,最终事件未出现在“ tbl_eventhistory”中):
http://icecream.me/b459c9d17854b081a0804692f67c3aa3
我知道这可能是最有效的方法,但是最高兴的是它经过很长的时间试图解决,现在可以工作了。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句