C
C#3mo ago
papijotaro.

Need a hand with Asp.net MVC Net7.

I totally forgot that I had an assignment due to Wednesday (in 3 days) because I was prioritizing other subjects. My teacher told me to create a forum-like website where users who are logged in will be able to post and their author name, posted date, etc will be visible. Also people who are logged in can comment on your post and their name and date of posting that comment will also be visible. The firsttask is basically that I have to make use of three Models, and all of them three independently interact with each other. ------------------- The other task is that I have to hash/encrypt any valuable data such as passwords which I already have completed. As mentioned above, both the post and comments and names will be in the same view. I have been trying to find anything valuable as a starting point but with multiple models in a single view but none. Even GPT doesnt help. If anyone can help a brother out. Really struggling lmfao. I can share any info you want let me know.
33 Replies
Angius
Angius3mo ago
What exactly is the issue?
papijotaro.
papijotaro.3mo ago
I need to create a post/blog site consisting of minimum three models that interact with eachother. Must be a blog themed site he said,
SpReeD
SpReeD3mo ago
So, how does your Models look like? As I understand it, it's plain MVC OOP, determine which Models to create and Properties they gonna have. User, Comment, etc.
Angius
Angius3mo ago
So a blog or a forum?
papijotaro.
papijotaro.3mo ago
By my definition a site where everyone can post and comment on eachothers stuff. He used the term "blog" but If I understood you correctly I already got 1 Model for Login, the user stuff basically, 1 for posting (title, description, etc) and 1 for comments, commentId, comment,
Angius
Angius3mo ago
That seems fine, yeah User, Post, Comment Post has author (User), Comment has author (User), Post has Comments
papijotaro.
papijotaro.3mo ago
Ye Thats the bare minimum for max score
SpReeD
SpReeD3mo ago
I think that doesn't matter, what's meant is, how you create the classes in MVC. For instance, having a Comment class, what does it need? - Properties like, Value as string, DateTime posted on, and which user posted it, so UserId as int. This Model correlate with the User model which has properties like Id as int, name as string, etc. - also it has a 1:1 relationship involved, etc.
papijotaro.
papijotaro.3mo ago
Yeah exactly, i think he means as in, basically have 3 models in a single view and make sure they all have the same purpose i.e( They all build up the posting mechanism) 1 does the comments part, 1 does user (author), createDate, (1 does data display of Title, and description)
SpReeD
SpReeD3mo ago
Maybe creating an ERM first, would help and giving some extra points.
papijotaro.
papijotaro.3mo ago
What does that stand for
SpReeD
SpReeD3mo ago
Entity Relationship Model
No description
papijotaro.
papijotaro.3mo ago
Yeah I understand. I dont know where to start you get me? I cant find anything
SpReeD
SpReeD3mo ago
AS @ZZZZZZZZZZZZZZZZZZZZZZZZZ mentioned earlier, create things/classes like User, Post, Comment, and think of them as entities, what kind of properies and Comment can or should have. That's also how to write an ERM or ERD.
Angius
Angius3mo ago
1. Make a folder named Models 2. Add files for User, Post, Comment classes 3. Add properties to those classes For example, a Post would be, say,
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public User Author { get; set; }
// etc.
}
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public User Author { get; set; }
// etc.
}
papijotaro.
papijotaro.3mo ago
Yes but how do I come to even be able to use them all three in the same view. My problems at the moment: 1. How to use 3 models in the same view. (Google havent been able to help at all seriously) if you're a better google'er go for it. 2. Lets say I found out how to solve problem 1. How do I build a bridge between those models, so when the author creates a post and I as a viewer click on it how am i supposed to write the logic so it's bound to that specific post? I need to mark them with their ids, to identify for each post, and thats the part i suck at. I understand the theoretical part but the physical is where im struggling at, because there is no info anywhere on how I can proceed.
Angius
Angius3mo ago
I assume you use a database? With Entity Framework?
papijotaro.
papijotaro.3mo ago
Yes MSSQL yes
Angius
Angius3mo ago
Cool Well, see how in my example the post has a property of type User?
SpReeD
SpReeD3mo ago
They have nothing to do with the view itself. The Controller part of MVC will do the job.
Angius
Angius3mo ago
That It allows you to load related data
papijotaro.
papijotaro.3mo ago
Yes
Angius
Angius3mo ago
For example,
var post = await _dbContext.Posts
.Where(p => p.Id == id)
.Include(p => p.Author)
.Include(p => p.Comments)
.FirstOrDefaultAsync();
var post = await _dbContext.Posts
.Where(p => p.Id == id)
.Include(p => p.Author)
.Include(p => p.Comments)
.FirstOrDefaultAsync();
papijotaro.
papijotaro.3mo ago
Is that User another model
Angius
Angius3mo ago
This would be a quick and very dirty way to load the post, that post's author, and the comments to that post
papijotaro.
papijotaro.3mo ago
which you reffered to
Angius
Angius3mo ago
Yes Three models: post, user, comment
papijotaro.
papijotaro.3mo ago
Where is this supposed to be Controller?
SpReeD
SpReeD3mo ago
Imho, there is a thin line between those three patterns; there are not so different and most of the time mixed together, If you take a look at these patterns, and your code, your entities/models, does it make sense, the connections?
No description
Angius
Angius3mo ago
It's the easiest way, yes
papijotaro.
papijotaro.3mo ago
Alright, can I fix this in 2 days you reckon? Because after all this i have still been trying to fix it but i dont know what code to write.
Angius
Angius3mo ago
Maybe
papijotaro.
papijotaro.3mo ago
my brain is so fried bro Been on visual studio for 10 hours staring into the abyss I need a sample or example so i can understand how its applied Maybe i can send you repo idk take a look
Want results from more Discord servers?
Add your server
More Posts