Я пытаюсь вернуть комментарии в цикле foreach в частичном представлении.
Однако я получаю сообщение об ошибке:
The cast to value type 'System.Int32' failed because the materialized
value is null. Either the result type's generic parameter or the query
must use a nullable type.
Я думаю, что это сообщение, потому что я не говорю частичное представление, какой идентификатор я ищу, хотя я не уверен.
Моя первоначальная попытка не работает.
@{Html.RenderAction("_Comments");}
Итак, я попробовал:
@{Html.RenderAction("_Comments", Model.TicketID);}
продолжает получать то же сообщение об ошибке.
Мой контроллер:
public ActionResult ViewTickets(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Ticket Ticket = db.Tickets.Find(id);
if (Ticket == null)
{
return HttpNotFound();
}
return View(Ticket);
}
public PartialViewResult _Comments(int? id)
{
var CommentViewTicket = (from s in db.Tickets
join cp in db.Comments on s.TicketID equals cp.TicketID into AP
from subComment in AP.DefaultIfEmpty()
select new CommentViewTicket
{
TicketID = s.TicketID,
TicketOwner = s.TicketOwner,
CommentID = subComment.CommentID,
CommentOwnerID = subComment.CommentOwnerID,
CommentDate = subComment.CommentDate,
CommentDescription = subComment.CommentDescription ?? String.Empty
});
return PartialView("_Comments",CommentViewTicket);
}
Страница моего просмотра:
@model ITHelpdesk.Areas.Support.Entities.Ticket
@{
ViewBag.Title = "Ticket";
}
<!-- /#breadcrumbs-->
<div id="page-header-container">
<div class="container">
<div class="page-header row">
<div class="span8">
<h1>Ticket #@Html.DisplayFor(model => model.TicketID)</h1>
</div>
<div class="span4">
</div>
</div>
</div>
</div>
<!-- /#breadcrumbs-->
<div id="wrap" class="container" role="document">
<div id="content" class="row">
< div >
< br />
< article class="post-15 page type-page status-publish hentry" />
<header>
<h1 class="ticket-title">@Html.DisplayFor(model => model.Title)</h1>
</header>
<dl class="details">
<dt class="odd">Opened:</dt>
<dd>@Html.DisplayFor(model => model.TimeStamp, "{0:d}")</dd>
<dt class="even">Last updated:</dt>
<dd>22/05/2018 at 11:36 am</dd>
<dt class="odd">Response Time:</dt>
<dd>1 Hour</dd>
<dt class="even">@Html.DisplayNameFor(model => Model.TicketPriority.Priority):</dt>
<dd class="priority-high">@Html.DisplayFor(model => model.TicketPriority.Priority)</dd>
<dt class="odd">@Html.DisplayNameFor(model => model.TicketType.Type):</dt>
<dd>@Html.DisplayFor(model => model.TicketType.Type)</dd>
<dt class="even">@Html.DisplayNameFor(model => model.TicketStatus.Status):</dt>
<dd class="status-new">@Html.DisplayFor(model => model.TicketStatus.Status)</dd>
</dl>
</div>
<div class="input-text">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextAreaFor(model => model.Description, 5, 10, new
{
htmlAttributes = new
{
@class = "input-text",
id = "comment",
placeholder = "Describe the problem, including details such as how to reproduce it and what version you are using."
}
})
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
@{Html.RenderAction("_Comments", Model.TicketID);}
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type = "submit" value="Create" class="btn btn-default" />
</div>
</div>
<div class="top-button"><a class="btn btn-default" href='@Url.Action("", "Tickets")'>Return to MyTickets</a></div>
<br />
Моя _Comments Page:
@model IEnumerable<ITHelpdesk.Areas.Support.Models.CommentViewTicket>
<link href="~/Content/flexbox.css" rel="stylesheet" type="text/css" />
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title></title>
</head>
<body>
<div>
<ul class="comment-section">
@foreach (var item in Model)
{
<li class="comment author-comment">
<div class="info">
<a href="#">@Html.DisplayFor(modelItem => item.CommentOwnerID)</a>
<span>3 hours ago</span>
</div>
<a class="avatar" href="#">
<img src="images/avatar_author.jpg" width="35" alt="Profile Avatar" title="Jack Smith" />
</a>
<p>@Html.DisplayFor(modelItem => item.CommentDescription)</p>
</li>
}
<li class="comment user-comment">
<div class="info">
<span>4 hours ago</span>
</div>
<a class="avatar" href="#">
<img src="images/avatar_user_1.jpg" width="35" alt="Profile Avatar" title="Anie Silverston" />
</a>
<p>Suspendisse gravida sem?</p>
</li>
<li class="comment author-comment">
<div class="info">
<a href="#">Jack Smith</a>
<span>3 hours ago</span>
</div>
<a class="avatar" href="#">
<img src="images/avatar_author.jpg" width="35" alt="Profile Avatar" title="Jack Smith" />
</a>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse gravida sem sit amet molestie portitor.</p>
</li>
</ul>
</div>
</body>
</html>