"Create new"按钮依赖于 ID



所以我需要做一个关于在线池的网络应用程序,池的重点是我能够创建问题和答案,现在我所做的只是,创建/编辑/删除一个问题,所有这些都在我的控制器中完成,但我遇到了对答案做同样的事情的问题, 因为答案只能通过问题 ID 添加,我不知道如何将 id 和其他东西联系起来进行创建。 所以这是我的代码:

添加答案(获取(:

public ActionResult AddAnswer(int id, string Text)
{
using (var poolDbContext = new PoolContext())
{
Question question = poolDbContext.Questions.Find(id);
return View(question);
}
}

这也是我的帖子:

[HttpPost, ActionName("AddAnswer")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> AddAnswerPost([Bind("Answers")] Question question, Answer answer, int id,string Text)
{
try
{
if (ModelState.IsValid)
{
using (var poolDbContext = new PoolContext())
{
Question questionid = poolDbContext.Questions.Find(id);
answer.Id = question.Answers.Count() + 1;  //dá o valor do novo id da resposta ao answers.Id
answer.Text = question.Answers.ToString();// devia dar o valor da resposta que foi enviada no formulario. "System.Collections.Generic.List`1[PoolManager.Models.Answer]"

poolDbContext.Answers.Add(answer);
Text =question.Answers.ToString();
poolDbContext.Questions.Add(questionid);
poolDbContext.Questions.Add(question);
await poolDbContext.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
}
}
catch (DbUpdateException /* ex */)
{
//Log the error (uncomment ex variable name and write a log.
ModelState.AddModelError("", "Unable to save changes. " +
"Try again, and if the problem persists " +
"see your system administrator.");
}
return View(question);
}

可能我的帖子都是错误的,但我跳某人可以帮助我改变它。

这也是我的问题模型:

public class Question
{
public Question()
{
Answers = new List<Answer>();
}
public int Id { get; set; }
public string Text { get; set; }
public List<Answer> Answers { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public Boolean Active { get; set; }
}

这是我的答案模型:

public class Answer
{
public int Id { get; set; }
public string Text { get; set; }
public int Votes { get; set; }

}

}

我已将我的 Get 方法更改为:

public ActionResult AddAnswer(int id)
{
using (var poolDbContext = new PoolContext())
{
var questionFromDb = poolDbContext.Questions.Include(q => q.Answers).Single(q => q.Id == id);
return View(questionFromDb);
}
}

还有我的发布方法:

HttpPost, ActionName("AddAnswer")]
[ValidateAntiForgeryToken]
public ActionResult AddAnswerPost(int QuestionID, string AnswerText)
{
try
{
if (ModelState.IsValid)
{
using (var poolDbContext = new PoolContext())
{
var questionFromDb = poolDbContext.Questions.Include(q => q.Answers).Single(q => q.Id == QuestionID);
questionFromDb.Answers.Add(new Answer() { Text = AnswerText });
poolDbContext.SaveChanges();
return RedirectToAction("Details", "Home", new { id = QuestionID });
}
}
}
catch (DbUpdateException /* ex */)
{
//Log the error (uncomment ex variable name and write a log.
ModelState.AddModelError("", "Unable to save changes. " +
"Try again, and if the problem persists " +
"see your system administrator.");
}
return View();
}

我的观点也有问题,正确的方法是这个:

<form asp-action="AddAnswer">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label class="control-label">@Model.Text</label>
<input type="hidden" name="QuestionID" value="@Model.Id" />
<label asp-for="Text" class="control-label"></label>
<input type="Text" name="AnswerText" />
<span asp-validation-for="Text" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</form>

最新更新