Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8265801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:55:53+00:00 2026-06-08T04:55:53+00:00

I have a view that makes use of a list and a string value.

  • 0

I have a view that makes use of a list and a string value.

Currently I am doing the following and passing it to the view:

var Note = (from p in db.tblNote where p.NoteName = pnote  select  p).ToList();
ViewBag.NotesID = id;

return PartialView(Note);

As you can see, currently I am passing the Note (which is a list) to the View and then will be getting the ViewBag directly in my view.

What I like to do is to create a viewmodel for this.
I like to know what the best practice is to create a viewmodel for what I need.

Here is what I came up for the ViewModel :

public class NotesViewModel
{        
    public string NotesID { get; set; } // this will replace where I had ViewBag.NotesID

    public IEnumerable< Notes>  NotesList { get; set; } // this will replace where I had var Note = (from p in db.tblNote      
}

I am somewhat lost on where I create the Notes that you see in IEnumerable<Notes>, do I create another class called Notes in a different .cs file and how do I assign the appropriate type to it.
The will represent the the LINQ Query.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T04:55:55+00:00Added an answer on June 8, 2026 at 4:55 am

    I assume you want Notes to be a List of type Note? If so then why not just do IEnumerable<Note> ?

    A IEnumerable is basically a generic data type that allows you to enumerate over its items, a List an extension of this.

    So you would just change your code to be :-

    public class NotesViewModel
    {        
    
        public string NotesID { get; set; } // this will replace where I had ViewBag.NotesID
    
        public IEnumerable<Note>  NotesList { get; set; } // this will replace where I had var Note = (from p in db.tblNote
    
    }
    
    public ActionResult MyFunction()
    {
        var Notes = (from p in db.tblNote where p.NoteName = pnote 
                select  p).ToList();
    
        var oMyNotesVM = new NotesViewModel();
        oMyNotesVM.NotesID = id;
        oMyNotesVM.NotesList = Notes;
    
        return PartialView(oMyNotesVM );
    }
    

    So here all we’ve done is take the List and passed it to IEnumerable since IEnumerable is a generic form of List. Basically for an item to impliment the IEnumerable it needs to support iteration through its datamembers.. for example

    while(enumerator.MoveNext())
    {
      object obj = enumerator.Current;
      // work with the object
    }
    

    Which is the same (well almost as)

    foreach(object obj in collection)
    {
        // work with the object
    }
    

    If your asking what the datatype for Note should be then that would be the class type of db.tblNote. If this is entity framework then you could just use the class that was auto-generated from your model for this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view that takes data from my site and then makes it
I have a view that renders a list elements using the following template :
I have this method in App Delegate that makes a window and content view
I have a table view that displays a list that I want the user
Revised: I have a list items on the page that I'm creating from database
I have a view that - among other things - contains a list of
let us assume that I have a reusable business layer that further makes use
I have a list view class that just like NSCollectionView requires an additional prototype
I have a view that has a column to make it sortable. When clicking
I have a MS SQL view that I want to make available as a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.