我添加了两个带有问题和答案的模型类,其中一个问题没有答案
public class Question
{
[Key]
public long IdQuestion { get; set; }
[Required]
[MaxLength(100)]
public string? Name { get; set; }
[Required]
[MaxLength(500)]
public string? Description { get; set; }
public List<Answer>? Answers { get; set; } = new List<Answer>();
}
public class Answer
{
public Answer() { }
[Key]
public long IdAnswer { get; set; }
[MaxLength(100)]
public string? Name { get; set; }
[MaxLength(500)]
public string? Description { get; set; }
// Foreign key
[ForeignKey("Fk_Question")]
public long QuestionId { get; set; }
}
但是除了我使用外键时遇到的各种错误之外,最难的是构建剃刀页面的逻辑
我创建了两个页面,index.cshtml 和 answers.cshtml
<!DOCTYPE html>
<html>
<head>
<link href="~/lib/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<h3 class="bg-primary text-white p-2 m-2">Perguntas</h3>
<form method="post">
@Html.AntiForgeryToken()
<label>nome:</label>
<input type="text" name="Name" id="Name" class="form-control"/><br/>
<label>Pergunta:</label>
<input type="text" name="Description" id="Description" class="form-control" /><br />
<input type="submit" />
</form>
@foreach(Question q in Model.Questions)
{
<div class="card">
<div class="card-header">
@q.Name
</div>
<div class="card-body">
@q.Description
</div>
<div class="card-footer">
@q.IdQuestion
</div>
<div>
<a href="[email protected]"><button class="btn btn-link">Answer</button></a>
</div>
</div>
}
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<link href="~/lib/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<form method="post">
@Html.AntiForgeryToken();
<label for="Name">Name</label>
<input type="text" name="Name" id="Name" class="form-control" /><br/>
<div class="form-group">
<label for="Answer">Answer:</label>
<input type="text" placeholder="type your answer here" name="Description"
id="Description" class="form-control"/>
@*<input type="hidden" name="x" id="x" value="@x" />*@
</div>
<input type="submit" />
</form>
@* here it should display only the answers for this question *@
@foreach(Question q in Model.Questions)
{
@foreach(Answer a in q.Answers)
{
<p>@a.Name</p>
<p>@a.Description</p>
}
}
</body>
</html>
带有各自的代码隐藏文件:
public class IndexModel : PageModel
{
private DataContext _dataContext;
public IndexModel(DataContext dataContext)
{
_dataContext = dataContext;
}
public DbSet<Question> Questions => _dataContext.Questions;
public DbSet<Answer> Answers => _dataContext.Answers;
public IActionResult OnPost(Question q)
{
if(q!= null)
{
Questions.Add(q);
}
int v = _dataContext.SaveChanges();
return RedirectToPage();
}
}
public class AnswersModel : PageModel
{
DataContext dataContext;
static long questionid = 0;
public AnswersModel(DataContext dataContext)
{
this.dataContext = dataContext;
}
public DbSet<Answer> Answers => dataContext.Answers;
public DbSet<Question> Questions => dataContext.Questions;
// answer is coming empty
public IActionResult OnGet(Answer a)
{
questionid = long.Parse(Request.Query["q"]);
return Page();
}
public async Task<IActionResult> OnPost(Answer a,long x)
{
bool ok = false;
foreach (Question q in Questions)
{
if (q.IdQuestion == questionid)
{
q.Answers.Add(new Answer
{
Description = a.Description,
Name = a.Name,
QuestionId = questionid,
});
//dataContext.Questions.Add(q);
ok = true;
}
}
if(ok)
await dataContext.SaveChangesAsync();
return Page();
}
}
}
注意我将问题的 id 作为查询参数发送并静态保存,这显然是错误的