在Detail View ASP.NET Core中添加注释

时间:2018-04-12 12:00:17

标签: c# asp.net-core-2.0 razor-pages

我尝试在详细信息视图中添加注释,但在TextArea中输入一些文本时它不起作用。在

中投掷NullReferenceException
if (id != ticket.TicketId)
{
    return NotFound();
}

我创建了ViewModel - >包含(票证和评论)

<div class="form-group">
    <form asp-action="Details">
        <label asp-for="Comment" class="control-label"></label>
        <textarea asp-for="Comment.Content" class="form-control" placeholder="Add comment!"></textarea>
        <span asp-validation-for="Comment.Content" class="text-danger"></span>
        <input type="submit" value="Add comment" class="btn btn-default" />
    </form>
</div>

以下是我尝试提供一些文本,绑定到Comment的示例,这是我在Controller中的Detail方法:

public async Task<IActionResult> Details(int id, TicketCommentViewModel model)
{
    var ticket = model.Ticket;
    if (id != ticket.TicketId)
    {
        return NotFound();
    }

    var oldTicket = await _context.Tickets.SingleOrDefaultAsync(t => t.TicketId == ticket.TicketId);
    var comment = new Comment();
    var user = await GetCurrentUserAsync();

    oldTicket.Comments.Add(comment);

    comment.Content = model.Comment.Content;
    comment.SendTime = DateTime.Now;
    comment.TicketID = ticket.TicketId;
    comment.Ticket = ticket;
    comment.UserId = user.Id;
    comment.User = user;

    if (ModelState.IsValid)
    {
        try
        {
            await _context.Comments.AddAsync(comment);
            _context.Update(oldTicket);
            await _context.SaveChangesAsync();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!TicketExists(oldTicket.TicketId))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }
        return RedirectToAction(nameof(Index));
    }
    return View(model);
}

0 个答案:

没有答案