我已经阅读了几个关于这个主题的 SO 问题,但老实说,其中大多数对我来说都太复杂了。我对 ASP.NET mvc 很陌生。
我有一个示例 ASP.NET mvc 4 应用程序,它是通过遵循(并且稍微偏离)电影数据库教程创建的。它具有内置帐户位、实体框架(任何时候我更改任何东西都会很痛苦)以及我根据教程中的模型自己构建的 2 个模型:
1) 错误
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
namespace MasterDetailPractice.Models
{
public class Bug
{
public int BugID { get; set; }
public string BugTitle { get; set; }
public DateTime BugDate { get; set; }
public string BugStatus { get; set; }
[Column(TypeName = "ntext")]
[MaxLength]
public string BugDescription { get; set; }
}
public class BugDBContext : DbContext
{
public DbSet<Bug> Bugs { get; set; }
public DbSet<Comment> Comments { get; set; }
}
}
2) 评论
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
namespace MasterDetailPractice.Models
{
public class Comment
{
public int CommentID { get; set; }
public int BugID { get; set; }
public int UserID { get; set; }
public DateTime CommentDate { get; set; }
[Column(TypeName = "ntext")]
[MaxLength]
public string CommentText { get; set; }
}
}
当我运行我的应用程序时,我可以转到 /Project 并使用添加链接获取标准索引视图,我可以在其中添加错误。添加后,我会看到通常的编辑/详细信息/删除链接。
当我运行我的应用程序时,我还可以转到 /Comment 并使用添加链接获取标准索引视图,我可以在其中添加评论。添加后,我会看到通常的编辑/详细信息/删除链接。
到目前为止,我还好。CRUD 表格可以工作,但它们不能一起工作。
问题:
目前,为了使评论适用于错误,我必须在 /Comment/Create 表单中实际输入 BugID。然后评论都只能在 /Comment/ 路线上使用。
相反,我需要发生以下情况:
- “添加评论”表单应该自动知道要保存的 BugID,而无需用户输入。
- A master-detail presentation of the data: The /Comment/Index view should appear at the bottom of the /Bug/Edit and/or Bug/Details page and show only the Comments related to the current Bug.
- The "Add Comment" link should only appear from the /Bug/Edit or /Bug/Details page, so Comments are never added without relating to a Bug.
It's kind of amazing that I haven't been able to figure this out myself, after spending 3 days poring over every Google result and SO post I can find on the topic. That said, here I am, hoping to learn the simplest possible implementation of this.
Do I need to post more code (the Controllers, for example, or the Views) in order for this question to be properly answerable?
Looking forward to getting the slow-learning train to start pulling out of the station...