none
How to show all data from table section when the Active field value = true RRS feed

  • Question

  • Problem

    I need to show all data from table section where Active field = true in table section

    my code as following

    namespace UniversityData.Models
    {
    
       [Table("Section")]
        public partial class Section
        {
            public Section()
            {
              
            }
           
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    
            [Key]
    
            public int SecID { get; set; }
    
            [Required]
            [StringLength(50)]
            public string SecName{ get; set; }
            public bool Active { get; set; }
    
           
    
        }
    }

    my Index view as following

    <body>
        <div>
           
            <table>
                <tr><td>SectionID</td><td>SectionName</td></tr>
                @foreach (var item in Model)
                {
                    <tr><td>@item.SecID</td><td>@item.SecName</td></td></tr>
                }
            </table>
        </div>
    </body>

    In controller section in function index i write following

    How to make index function inside section controller show only all records from table section if value of Active field = true

      public ActionResult Index()
            {
                return View(db.Sections.ToList());
            }

    Wednesday, November 16, 2016 12:19 AM

All replies

  • Hi eng-ahmed-salah,

    According to your description, it seems that you want to retrieve the value of Active field = true, if it is the case, please add where conduction in your code, like this: 

    public ActionResult Index()
    {
         return View(db.Sections.Where(Where(x => x.Active == true).ToList());
    }

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 16, 2016 9:46 AM
    Moderator