0

I'm creating a MVC.NET 5 project and i got this error System.ArgumentNullException: 'Value cannot be null. Parameter name: source'. I researched that it is from an IEnumerable in a model.

public class EventDetailsViewModel
{
    public int Id { get; set; }
    
    public string Description { get; set; }

    public string AuthorId { get; set; }

    public IEnumerable<CommentViewModel> Comments { get; set; }

    public static Expression<Func<Event, EventDetailsViewModel>> ViewModel
    {
        get
        {
            return e => new EventDetailsViewModel()
            {
                Id = e.Id,
                Description = e.Description,
                AuthorId = e.AuthorId,
            };
        }
    }
}

Comment Model

public class CommentViewModel
    {
        public string Text { get; set; }

        public string Author { get; set; }

        public int EventId { get; set; }

        public static Expression<Func<Comment, CommentViewModel>> ViewModel
        {
            get
            {
                return c => new CommentViewModel()
                {
                    Text = c.Text,
                    Author = c.Author.FullName,
                    EventId = c.EventId
                };
            }
        }
    }

Controller

public PartialViewResult EventDetailsById(int id)
    {
        var currentUserId = this.User.Identity.GetUserId();
        var isAdmin = this.isAdmin();
        var eventDetails = this.db.Events.Where(e => e.Id == id)
            .Where(e => e.IsPublic || isAdmin || (e.AuthorId != null && e.AuthorId == currentUserId))
            .Select(EventDetailsViewModel.ViewModel)
            .FirstOrDefault();

        var isOwner = (eventDetails != null && eventDetails.AuthorId != null && eventDetails.AuthorId == currentUserId);
        this.ViewBag.CanEdit = isOwner || isAdmin;

        return this.PartialView("_EventDetails", eventDetails);
    }

View

@model Events.Web.Models.EventDetailsViewModel

@if (Model.Description != null)
{
    <div class="description">Description: @Model.Description</div>
}

@if (Model.Comments.Any())
{
    @:Comments:
    <ul>
        @foreach (var comment in Model.Comments)
        {
            <li>
                @comment.Text
                @if (@comment.Author != null)
                {
                    @: (by @comment.Author)
                }
            </li>
        }
    </ul>
}
else
{
    <p>No comments</p>
}

I want both Event detail and Comments are displayed but it seemed not working. Database work fine., i can see all of the comment items in the database but they are not displayed here.

0 Answers0