© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4mo ago•
108 replies
strikeouts27

✅ Understanding how to build controllers.

My controller is supposed to pass ifnormation from the model to the view. My instructor left some code for me to understand. I also wrote out his comments. Can someone help me explain these parts to me?

// Question 1 So the first thing that we do is connect the model to the controller correct? Is that what is happening on this line of code? 
// Question 2 I am lost on what context is. Based on my googling this is objects that 
// Question 3 
//Question 4 is there a way to see the context data as is before formatting? 
// Question 5 Iqueryable what is that? 

//ViewBag collects information from the models for the view to access correct? 

// HomeController.cs 
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using FAQ.Models;
using Microsoft.AspNetCore.Http.Features;
using System.Linq; 

namespace FAQ.Controllers;

public class HomeController : Controller
{
    // I logger provides logging information for the controller. 
    private readonly ILogger<HomeController> _logger;

    public HomeController(ILogger<HomeController> logger)
    {
        _logger = logger;
    }

    //ASP.NET IActionResult documentaiton: https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.iactionresult?view=aspnetcore-9.0#definition
    // IActionResult provides information to MVC about the type of the HTTP response an action method should return.  
    public IActionResult Index()
    {
        return View();
    }

    public IActionResult Privacy()
    {
        return View();
    }


    [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
    public IActionResult Error()
    {
        return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
    }

    // Code Example Provided By Professor

} 
    //  The controller retrives FAQS from the database and applies filtering logic. 
    // The home controller is responsible for retrieving faq
    // The controller is responsible for retrieving FAQ's and applying filtering logic.
    // Here is what happens inside of the controller.   
    // It filters the topics based on the selected topic and category. 
    // it than passes the data to the view using viewbag. 
    // the index action retrieves faqs and related topics and categories. 
    // if a topic is selected, it is filtered with the .where method. 
    // if the the category is selected than the faqs are further refined using the the .where method.   
    // The filtred list is passed to the views. 
    
// Question 1 So the first thing that we do is connect the model to the controller correct? Is that what is happening on this line of code?  
    private FaqsContext context { get; set; }

// Question 2 I am lost on what context is. Based on my googling this is objects that have been created correct? 
    public HomeController (FaqsContext ctx)
    {
        context = ctx;
    }

// Question 3 
//ViewBag conllects information from the models for the view to access correct? 
//Question 4 is there a way to see the context data as is before formatting? 

        ViewBag.Topics = context.Topics.OrderBy(t => t.Name).ToList();
        ViewBag.Categories = context.Categories.OrderBy(c => c.Name).ToList();

// Question 5 Iqueryable what is that? 
        IQueryable<FAQ> faqs = context.FAQs
            .Include(f => f.Topic)
            .Include(f => f.Category)
            .OrderBy(f => f.Question);
        if (!string.IsNullOrEmpty(topic))
        {
            faqs = faqs.Where(f = false.TopicId == topic);
        }
        if (!string.IsNullOrEmpty(category)) {
            faqs = faqs.Where(f => f.CategoryId == category);
        }

        return View(faqs.ToList()); 
    }
}
// Question 1 So the first thing that we do is connect the model to the controller correct? Is that what is happening on this line of code? 
// Question 2 I am lost on what context is. Based on my googling this is objects that 
// Question 3 
//Question 4 is there a way to see the context data as is before formatting? 
// Question 5 Iqueryable what is that? 

//ViewBag collects information from the models for the view to access correct? 

// HomeController.cs 
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using FAQ.Models;
using Microsoft.AspNetCore.Http.Features;
using System.Linq; 

namespace FAQ.Controllers;

public class HomeController : Controller
{
    // I logger provides logging information for the controller. 
    private readonly ILogger<HomeController> _logger;

    public HomeController(ILogger<HomeController> logger)
    {
        _logger = logger;
    }

    //ASP.NET IActionResult documentaiton: https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.iactionresult?view=aspnetcore-9.0#definition
    // IActionResult provides information to MVC about the type of the HTTP response an action method should return.  
    public IActionResult Index()
    {
        return View();
    }

    public IActionResult Privacy()
    {
        return View();
    }


    [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
    public IActionResult Error()
    {
        return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
    }

    // Code Example Provided By Professor

} 
    //  The controller retrives FAQS from the database and applies filtering logic. 
    // The home controller is responsible for retrieving faq
    // The controller is responsible for retrieving FAQ's and applying filtering logic.
    // Here is what happens inside of the controller.   
    // It filters the topics based on the selected topic and category. 
    // it than passes the data to the view using viewbag. 
    // the index action retrieves faqs and related topics and categories. 
    // if a topic is selected, it is filtered with the .where method. 
    // if the the category is selected than the faqs are further refined using the the .where method.   
    // The filtred list is passed to the views. 
    
// Question 1 So the first thing that we do is connect the model to the controller correct? Is that what is happening on this line of code?  
    private FaqsContext context { get; set; }

// Question 2 I am lost on what context is. Based on my googling this is objects that have been created correct? 
    public HomeController (FaqsContext ctx)
    {
        context = ctx;
    }

// Question 3 
//ViewBag conllects information from the models for the view to access correct? 
//Question 4 is there a way to see the context data as is before formatting? 

        ViewBag.Topics = context.Topics.OrderBy(t => t.Name).ToList();
        ViewBag.Categories = context.Categories.OrderBy(c => c.Name).ToList();

// Question 5 Iqueryable what is that? 
        IQueryable<FAQ> faqs = context.FAQs
            .Include(f => f.Topic)
            .Include(f => f.Category)
            .OrderBy(f => f.Question);
        if (!string.IsNullOrEmpty(topic))
        {
            faqs = faqs.Where(f = false.TopicId == topic);
        }
        if (!string.IsNullOrEmpty(category)) {
            faqs = faqs.Where(f => f.CategoryId == category);
        }

        return View(faqs.ToList()); 
    }
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements
Next page

Similar Threads

✅ How to handle errors in services/controllers
C#CC# / help
2y ago
Controllers purpose
C#CC# / help
4y ago
❔ How to add controllers into blazor hybrid app?
C#CC# / help
3y ago
how to build solution
C#CC# / help
2y ago