locked
Display detail by using ModelView RRS feed

  • Question

  • User974979776 posted

    Hi, I'm using model view to display two model in one view. When I click on link to display detail I get error.

    This is my code

    namespace Knowledge.ViewModels
    {
        public class ArticleCommentVM
        {
            public IEnumerable<Article> Articles { get; set; }
            public IEnumerable<Comment> Comments { get; set; }
    
        }
    }
    // GET: Articles/Details/5
    public ActionResult Detail(int id)
    {
    ArticleCommentVM modelVM = new ArticleCommentVM();
    modelVM.Articles = db.Articles.Find(id);
    return View();
    }

    The error in this line: modelVM.Articles = db.Articles.Find(id); How can solve this problem? thanks

    Thursday, April 23, 2020 9:07 AM

All replies

  • User475983607 posted

    Find(int) returns a single type not a collection.   Either change the ViewModel or change the LINQ query.   The following class shapes seems like a better representation of Article comments.  Also, always include the error when posting on the forum so the community is not forced to guess.

    public class ArticleCommentVM
    {
    	public Article Article { get; set; }
    	public IEnumerable<Comment> Comments { get; set; }
    
    }
    // GET: Articles/Details/5 
    public ActionResult Detail(int id) 
    { 
      ArticleCommentVM modelVM = new ArticleCommentVM();
      modelVM.Article = db.Articles.Find(id);
      return View(); 
    } 

    Thursday, April 23, 2020 10:42 AM
  • User974979776 posted

    Thank you mgebhard,

    When I changed ViewModel, I got a new error with this action:

    // GET: ArticleComment

    public ActionResult Index()

    {

    var mymodel = new ArticleCommentVM();

    mymodel.Articles = db.Articles.Where(a => a.FirstPageArticle == true).Include(a => a.Sort).Include(a => a.Writer).ToList();

    return View(mymodel);

    }

    How can it be solved?

    Thursday, April 23, 2020 12:10 PM
  • User475983607 posted

    mubarak200

    Thank you mgebhard,

    When I changed ViewModel, I got a new error with this action:

    // GET: ArticleComment

    public ActionResult Index()

    {

    var mymodel = new ArticleCommentVM();

    mymodel.Articles = db.Articles.Where(a => a.FirstPageArticle == true).Include(a => a.Sort).Include(a => a.Writer).ToList();

    return View(mymodel);

    }

    How can it be solved?

    Let me understand.  You did not follow my recommendation of fixing the model which allows Articles can have related comments. You change the to a LINQ query and are receiving a new error but decided to not share the error.   

    I can see that you are missing a select and I'm sure the error message is telling exactly what the issues is.

    Thursday, April 23, 2020 12:27 PM
  • User974979776 posted

    I changed ViewModel to this shape:

    namespace Knowledge.ViewModels
    {
        public class ArticleCommentVM
        {
            public Article Articles { get; set; }
            public IEnumerable<Comment> Comments { get; set; }
    
        }
    }
    

    An error occurred in the action of index and this message appears:

    CS0029 Cannot implicitly convert type 'System.Collections.Generic.List<Knowledge.Models.Article>' to 'Knowledge.Models.Article'       

    Thursday, April 23, 2020 12:51 PM
  • User475983607 posted

    Seriously?  You created the opposite problem of your first post.  Your first post tries to assign a single type to a collection type.   You're new code tries to assign a collection to a single type.  

    Frankly, your design direction is not clear.  What is clear is you do not understand he difference between a Collection and a single type.  See the C# programming guide.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/collections

    Thursday, April 23, 2020 1:01 PM
  • User1120430333 posted
    You can't take a List object that holds many Article objects that it's now going to be an Article object. The Article object can't hold a List.
    Thursday, April 23, 2020 1:22 PM
  • User974979776 posted

    I deleted the list and still the same error.

    Thursday, April 23, 2020 1:46 PM
  • User974979776 posted

    The topic takes time to read, I will read it later, but now I want to solve my problem.

    Thursday, April 23, 2020 1:49 PM
  • User-474980206 posted

    C# is a strongly typed language. If you don’t understand types, you will not be able to code properly or understand the very clear error messages you are getting. Also if you are going to use linq, you will need to learn generic types and their restrictions of anonymous types.

    Thursday, April 23, 2020 2:48 PM
  • User1120430333 posted

    mubarak200

    I deleted the list and still the same error.

    Anytime you do a Linq-2-Entities query, the results returned are going to be a List of objects.

    public Article Articles { get; set; }  // the definition is wrong if you try to point the results of a Linq query to it, becuase it's returning a List.

    It should be public Ienumerable<Article> Articles {get; set:}  // it works with the List.

    Thursday, April 23, 2020 7:24 PM
  • User-17257777 posted

    Hi mubarak200,

    You can simply define two ViewModels:

    public class ArticleCommentVM1
        {
            public IEnumerable<Article> Articles { get; set; }
            public IEnumerable<Comment> Comments { get; set; }
    
        }
    public class ArticleCommentVM2
        {
            public Article Article { get; set; }
            public IEnumerable<Comment> Comments { get; set; }
    
        }

    The VM1 is for Index action and VM2 is for Detail action.

    Best Regards,

    Jiadong Meng

    Friday, April 24, 2020 2:26 AM
  • User974979776 posted

    Your idea is great but I got this error:

    Compiler Error Message: CS1579: foreach statement cannot operate on variables of type 'Article' because 'Article' does not contain a public instance definition for 'GetEnumerator'

    @using Knowledge.ViewModels
    @model ArticleCommentVM2
    
    //The error here..in this line
    
    @foreach (var item in Model.Articles)
          {
    
    

    Friday, April 24, 2020 6:10 AM
  • User1120430333 posted

    Your idea is great but I got this error:

    Compiler Error Message: CS1579: foreach statement cannot operate on variables of type 'Article' because 'Article' does not contain a public instance definition for 'GetEnumerator'

    @using Knowledge.ViewModels
    @model ArticleCommentVM2
    
    //The error here..in this line
    
    @foreach (var item in Model.Articles)
          {
    

    https://docs.microsoft.com/en-us/dotnet/api/system.collections.ienumerable.getenumerator?view=netframework-4.8

    @foreach (var item in Model.Articles // You may be calling it Articles think it's a collection of Article objects, but .NET sees it as Article and not a List object holding Article objects that it can iterate over using a for loop.

    Friday, April 24, 2020 8:04 AM
  • User974979776 posted

    Thank you so much

    Friday, April 24, 2020 9:22 AM