【问题标题】:How to return the view result of another action in the same asp.net core mvc controller如何在同一个asp.net core mvc控制器中返回另一个动作的视图结果
【发布时间】:2017-05-03 04:21:44
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

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

    return View(nameof(Show), question);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-12
      • 2021-11-28
      • 2021-06-21
      • 2020-01-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多