如何修复部分视图以返回结果

时间:2019-04-19 11:29:47

标签: razor asp.net-mvc-5 entity-framework-6

我试图在局部视图的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.

我认为此消息是因为虽然不确定,但我没有告诉局部视图我要查找的ID。

我最初的尝试无效。

 @{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);
        }

我的ViewTicket页面:

@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 />

我的_评论页面:

@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>

1 个答案:

答案 0 :(得分:0)

解决方案是更改以下代码:

控制器:

public PartialViewResult _Comments(int id)
        {


            var CommentViewTicket = (from s in db.Comments
                                       where s.TicketID ==id

                                       select new CommentViewTicket
                                       {
                                           TicketID = s.TicketID,
                                           CommentID = s.CommentID,
                                           CommentOwnerID = s.CommentOwnerID,
                                           CommentDate = s.CommentDate,
                                           CommentDescription = s.CommentDescription
                                       });



            return PartialView(CommentViewTicket);

我的ViewTicket页面:

@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>

    @{

        int TicketID = Model.TicketID;

        Html.RenderAction("_Comments");

    }



            <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 />

 </div>