C
C#8mo 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
Angius8mo ago
What exactly is the issue?
papijotaro.
papijotaro.OP8mo 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
SpReeD8mo 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
Angius8mo ago
So a blog or a forum?
papijotaro.
papijotaro.OP8mo 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
Angius8mo ago
That seems fine, yeah User, Post, Comment Post has author (User), Comment has author (User), Post has Comments
papijotaro.
papijotaro.OP8mo ago
Ye Thats the bare minimum for max score
SpReeD
SpReeD8mo 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.OP8mo 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
SpReeD8mo ago
Maybe creating an ERM first, would help and giving some extra points.
papijotaro.
papijotaro.OP8mo ago
What does that stand for
SpReeD
SpReeD8mo ago
Entity Relationship Model
No description
papijotaro.
papijotaro.OP8mo ago
Yeah I understand. I dont know where to start you get me? I cant find anything
SpReeD
SpReeD8mo 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
Angius8mo 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.OP8mo 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
Angius8mo ago
I assume you use a database? With Entity Framework?
papijotaro.
papijotaro.OP8mo ago
Yes MSSQL yes
Angius
Angius8mo ago
Cool Well, see how in my example the post has a property of type User?
SpReeD
SpReeD8mo ago
They have nothing to do with the view itself. The Controller part of MVC will do the job.
Angius
Angius8mo ago
That It allows you to load related data
papijotaro.
papijotaro.OP8mo ago
Yes
Angius
Angius8mo 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.OP8mo ago
Is that User another model
Angius
Angius8mo 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.OP8mo ago
which you reffered to
Angius
Angius8mo ago
Yes Three models: post, user, comment
papijotaro.
papijotaro.OP8mo ago
Where is this supposed to be Controller?
SpReeD
SpReeD8mo 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
Angius8mo ago
It's the easiest way, yes
papijotaro.
papijotaro.OP8mo 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
Angius8mo ago
Maybe
papijotaro.
papijotaro.OP8mo 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