Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Guys,
I am working on a forum and i want each topic under a category to have thier own seperaye view. For Instance, i have a Category named Technology and I want each post under the Category to have thier own view, how do i get to do that.


Also i have an e-Library, and i want each books description to have thier own view, but it seems all the description are all in one view.

Here are the forum models:

C#
namespace mouauforum_master.Models
{
    public class Category
    {
        public Category()
        {
            DateCreated = DateTime.Now;
        }
        public int CategoryId { get; set; }

        [Display(Name = "Category Name")]
        [Required(ErrorMessage = "Field is Required")]
        public string Categoryname { get; set; }

        [Display(Name = "Category Description")]
        public string Categorydescription { get; set; }

        public DateTime DateCreated { get; set; }

        public int GroupId { get; set; }
        public Group Group { get; set; }
    }
}


namespace mouauforum_master.Models
{
    public class Post
    {
        public Post()
        {
            DatePosted = DateTime.Now;
        }

        public int PostId { get; set; }
        [Required(ErrorMessage="Field is Required")]
        [Display(Name = "Post Title")]
        public string PostTopic { get; set; }

        [Required(ErrorMessage="Field is Required")]
        [Display(Name = "Post Subject")]
        public string PostSubject { get; set; }

        [Display(Name = "Commented By")]
        public string PostedBy { get; set; }

        public string PostFile { get; set; }

        [Display(Name = "Date Commented")]
        public DateTime DatePosted { get; set; }

        public int UserId { get; set; }
        public UserProfile UserProfiles { get; set; }

        public int CategoryId { get; set; }
        public Category Category { get; set; }

        public virtual ICollection<usercomment> UserComments { get; set; }

        public virtual ICollection<quote> Replies { get; set; }

    }
}
Posted
Updated 6-Dec-15 10:14am
v2

1 solution

Assuming you have list of all categories displayed to your user, you can make them links looking like that:

HTML
<a href="categories/<%: category.CategoryId %>" />
(or however you want to pass the CategoryId)

And in your Controller you can create an ActionResult that will be able to read the parameter and grab the model:

C#
// GET: /Categories/5

public ActionResult Categories(int? id)
{
    if (id != null)
    {
        var category = /* get from model repository by id */;

        return View(category);
    }
    else
    {
        return RedirectToAction("Index");
    }
}
 
Share this answer
 
v2
Comments
Member 12139499 6-Dec-15 16:39pm    
its how to get the category id that is my problem, can u please show more light on that

See my code here:

var topic = db.Posts.Include(p => p.Category).Where(x => x.Category.Categoryname.ToUpper() == id.ToUpper()).ToList();
dobravka 6-Dec-15 16:53pm    
Is this DB access line working fine? I see you compare CategoryName with CategoryId.

If it is, it looks like you are missing one thing - a list of Posts in Category Model. There you should put Posts filtered for that Category and then display it in a View. After that you do the same thing as in my previous response - redirect user to each Post by PostId.

Remember that these are ViewModels not just Models, so it should be designed in the way it is the best for you to display it.
Member 12139499 6-Dec-15 17:03pm    
I am really confused. My issue with the whole forum coding is the Id thing, like i cant make a post unless i have a dropdownlist of all my categories, and i dont like it. i want if a user enters any category, the Id is recognized and he can make a post without having to select from a dropdown.

So please if u can give me a sample code to make things clearer for me bro, i'd appreciate
dobravka 6-Dec-15 17:15pm    
First of all I assume you are using ASP.Net MVC, but you didn't tag it so I'm not even sure. Or I don't know if it is Razor engine or aspx?

When a user goes to Category View, the view is being displayed with Category model, so it has the Id there. You can access it in the View so there is no need to ask user.

And you don't have to display all form controls. Also it may be helpful to change the values you don't want user to fill to nullable. Than validation would pass even without the data.
Member 12139499 6-Dec-15 17:53pm    
Sorry i didnt include that, am using MVC4 Razor Syntax

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900