After many hours on Youtube, I have started my first test project. (.NET Core MVC) But I find it hard to 'connect the dots'.
The following code works, but I assume it could be (greatly?) improved...?
Basically I want to show a product category and products within.
I think loading the data could be improved? And I can't figure out how to implement an interface.
Any thoughts/pointers?
Controller:
public IActionResult Index()
{
var data = CollectionData.GetCollectionById(1);
return View(data);
}
DataAccess:
public class CollectionData
{
public static CollectionModel GetCollectionById(int id)
{
SqlDataAccess sql = new SqlDataAccess();
var p = new { id = id };
var output = sql.LoadData<CollectionModel, dynamic>("collection_get_by_id", p, "DefaultConnection");
output[0].CollectionProducts = sql.LoadData<CollectionProducts, dynamic>("products_get_by_collectionid", p, "DefaultConnection");
return output[0];
}
}
Models:
public class CollectionModel
{
public string Title { get; set; }
public string SubTitle { get; set; }
public List<CollectionProducts> CollectionProducts { get; set; }
}
public class CollectionProducts
{
public string Title { get; set; }
public decimal Price { get; set; }
}
I like the code to be 'best practice' for a complex solution - but without going 'over the top'
All pointers are greatly appreciated!
None
0 Points
1 Post
New to .NET - Is this the correct way?
Jan 28, 2021 09:39 PM|HrBagge|LINK
After many hours on Youtube, I have started my first test project. (.NET Core MVC) But I find it hard to 'connect the dots'.
The following code works, but I assume it could be (greatly?) improved...?
Basically I want to show a product category and products within.
I think loading the data could be improved? And I can't figure out how to implement an interface.
Any thoughts/pointers?
Controller:
DataAccess:
Models:
I like the code to be 'best practice' for a complex solution - but without going 'over the top'
All pointers are greatly appreciated!
All-Star
52971 Points
23574 Posts
Re: New to .NET - Is this the correct way?
Jan 28, 2021 09:55 PM|mgebhard|LINK
I recommend the Getting Started tutorials in this site. The tutorials cover best practices and standard programming patterns.
https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/?view=aspnetcore-5.0
https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/start-mvc?view=aspnetcore-5.0&tabs=visual-studio
Working with data
https://docs.microsoft.com/en-us/aspnet/core/data/ef-rp/intro?view=aspnetcore-5.0&tabs=visual-studio
https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/?view=aspnetcore-5.0