// 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());
}
}