gpt4 book ai didi

asp.net-core - 如何在同一个asp.net core mvc Controller 中返回另一个 Action 的 View 结果

转载 作者:行者123 更新时间:2023-12-05 02:19:00 25 4
gpt4 key购买 nike

我想显示 ShowByIdShow 方法的结果,但我得到 InvalidOperationException: The view 'ShowById' was not found

    public async Task<IActionResult> ShowById(int id) 
{
Expression<Func<Question, bool>> findById = (q) => q.QuestionId == id;
return await this.Show(findById);
}

private async Task<IActionResult> Show(
Expression<Func<Question, bool>> predExp)
{
var question = await _context.Questions
.Where(predExp)
.FirstOrDefaultAsync();

if (question == null) {
return NotFound();
}

question.Topics = await (
from topic in _context.Topics
join qtopic in _context.QuestionTopics on topic.TopicId equals qtopic.TopicId
where qtopic.QuestionId == question.QuestionId
select topic
).ToListAsync();

question.Answers = await _context.Answers
.Where(a => a.QuestionId == question.QuestionId)
.ToListAsync();

return View(question);
}

最佳答案

您必须明确指定 View 的名称。所以最后一行是:

return View(nameof(Show), question);

关于asp.net-core - 如何在同一个asp.net core mvc Controller 中返回另一个 Action 的 View 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43746426/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com