Return View(“ NameOfTheView”)不起作用

时间:2018-08-07 13:05:40

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

这是我试图返回特定视图的代码:

编辑:我使用AJAX调用此方法

 [HttpPost]
    public IActionResult PostData(SurveyData surveyData)
    {
        if (SaveRecords(surveyData))
        {
            logger.Write($"Data added to the database successfully!", LogEventLevel.Information);
            return View("ThankYouPage"); //separate view, same layout
        }
        else
        {
            logger.Write($"Failed adding data to the database!!", LogEventLevel.Error);
            return View("Error");
        }
    }

单击“提交”按钮后,我等待将数据写入数据库,然后显示“感谢页面”,这是一个静态cshtml页面,没有模型。

使用这种方法时,如果找到“数据”,我会得到“谢谢页面”:

 public async Task<IActionResult> Index(string data)
    {
        //check if data exists, return the thank you page
        var found = await _dbContext.SurveyData.AnyAsync(e => e.Data == data);
        if(found)
        {
            return View("ThankYouPage");
        }
        else
        {
            //do something else
            return View();
        }
    }

从控制台:

Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: 
Executed action method pmi.Controllers.HomeController.PostData (pmi), returned result Microsoft.AspNetCore.Mvc.ViewResult in 252.3692ms.
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor:Information: 
Executing ViewResult, running view ThankYouPage.
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor:Information: 
Executed ViewResult - view ThankYouPage executed in 6.2028ms.
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: 
Executed action pmi.Controllers.HomeController.PostData (pmi) in 284.9033ms
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request finished in 294.3209ms 200 text/html; charset=utf-8

我也不例外,那么可能出什么问题了吗? 区别在于,一种方法是异步的,而另一种则不是,那么这可能是原因吗?

谢谢。

编辑:

客户端代码:

$(function () {
$('#Button1').click(function () {
    var radiobutton1 = $('input[name="rb_answer1"]:checked').val();
    var answer1 = $('#answer1').val();
    var radiobutton2 = $('input[name="rb_answer2"]:checked').val();
    var answer2 = $("#answer2").val();
    var Abcde = $("#abcde").val();

    $.ajax({
        type: 'POST',
            url: '/home/postdata/',
        data: {
            ONMBR: Abcde,
            Answer1: radiobutton1,
            Answer11: answer1,
            Answer2: radiobutton2,
            Answer21: answer2 
        },

    });
});

})

1 个答案:

答案 0 :(得分:4)

您没有回调到AJAX,这当然意味着您实际上没有对响应做任何事情。 AJAX不会自动发生任何事情。负责响应(提供给回调)并对其进行处理,例如在DOM中选择一些内容以将返回的HTML插入其中。但是,这仅在返回部分PartialView时才有意义。返回View意味着您将获得完整的HTML文档,包括所有布局,标题等。

相关问题