✅ Understanding how to create a controller in ASP.NET.

I am trying to build my ASP.NET controller. I believe that its job is to take the data from the database and package it up and than make it available to the view. After reviewing the tutorial yesterday I discovered that using um dependency injection, I can drop objects into the controller and than it will return that object to the corresponding view. I need to package up all of the questions, answers, topics, and categories. I than need to allow the user to click on the different categories those related categories questions and answers will appear on the webpage. I have never built a controller before.
Category.cs
// Category.cs
namespace FAQ.Models
{
public class Category
{
// Entities are rows in a C# database.
public required string CategoryId { get; set; }
public required string Name { get; set; }
public required int FAQID { get; set; }

public required int TopicID { get; set; }
}

// make a category constructor and on model builder
}
Category.cs
// Category.cs
namespace FAQ.Models
{
public class Category
{
// Entities are rows in a C# database.
public required string CategoryId { get; set; }
public required string Name { get; set; }
public required int FAQID { get; set; }

public required int TopicID { get; set; }
}

// make a category constructor and on model builder
}
FAQ.cs
// FAQ.CS
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using FAQ.Models;

namespace FAQ.Models
{
public class QuestionAnswer
{
public required int Id {get; set;}
public required string Question {get; set;}
public required string Answer { get; set; }
public required string TopicId { get; set; } = null!;
public Topic Topic { get; set; } = null!;
public required string CategoryId { get; set; } = null!;
public Category Category { get; set; } = null!;
}
}
FAQ.cs
// FAQ.CS
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using FAQ.Models;

namespace FAQ.Models
{
public class QuestionAnswer
{
public required int Id {get; set;}
public required string Question {get; set;}
public required string Answer { get; set; }
public required string TopicId { get; set; } = null!;
public Topic Topic { get; set; } = null!;
public required string CategoryId { get; set; } = null!;
public Category Category { get; set; } = null!;
}
}
namespace FAQ.Models;

public class ErrorViewModel
{
public string? RequestId { get; set; }

public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
}
namespace FAQ.Models;

public class ErrorViewModel
{
public string? RequestId { get; set; }

public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
}
Topic.cs
// TopicModel.cs
namespace FAQ.Models

{
public class Topic
{
// must be a string per instructions
public required string Id { get; set; }
public required string Name { get; set; }
}

// make a topic constructor and on model builder
// public FAQModel() { } // EF-friendly parameterless ctor

// protected void OnModelCreating(ModelBuilder modelBuilder)
}
// TopicModel.cs
namespace FAQ.Models

{
public class Topic
{
// must be a string per instructions
public required string Id { get; set; }
public required string Name { get; set; }
}

// make a topic constructor and on model builder
// public FAQModel() { } // EF-friendly parameterless ctor

// protected void OnModelCreating(ModelBuilder modelBuilder)
}
2 Replies
strikeouts27
strikeouts27OP3w ago
!close
Accord
Accord3w ago
Closed!

Did you find this page helpful?