如何在同一个asp.net核心mvc控制器中返回另一个动作的视图结果

时间:2017-05-02 20:11:07

标签: asp.net-core asp.net-core-mvc

我想从Show显示以下ShowById方法的结果,但我得到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);
    }

1 个答案:

答案 0 :(得分:4)

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

return View(nameof(Show), question);