如何让每个论坛主题都有自己独立的视图
Hello Guys,
我正在论坛上工作,我希望某个类别下的每个主题都有自己的seperaye视图。对于Instance,我有一个名为Technology的类别,我希望Category下的每个帖子都有他们自己的视图,我该怎么做。
我也有一个电子图书馆,我希望每本书的描述都有自己的视图,但似乎所有描述都在一个视图中。
以下是论坛模型:
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:
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; }
}
}
假设您有显示给用户的所有类别的列表,你可以让它们看起来像这样:
Assuming you have list of all categories displayed to your user, you can make them links looking like that:
<a href="categories/<%: category.CategoryId %>" />
(或者你想要传递CategoryId)
在你的控制器中你可以创建一个能够读取参数并获取模型的ActionResult :
(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:
// 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");
}
}