Reputation: 162
This is the code where i am trying to return a specific view :
EDIT : I call this method using 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");
}
}
After clicking the "Submit" button, i wait for the data to be written in the database and then show the Thank You Page, which is a static cshtml page, without a model.
While on this method, if "data" is found, i do get the Thank You Page :
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();
}
}
From Console :
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
I get no exception, so what could be going wrong? The difference is that one method is async and one not, so could this be the reason?
Thank you in advance.
EDIT :
Client side code:
$(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
},
});
});
})
Upvotes: 0
Views: 69
Reputation: 239430
You have no callback to your AJAX, which then of course means you're not actually doing anything with the response. Nothing happens automatically with AJAX. It's on you to take the response (provided to the callback) and do something with it, such as select something in the DOM to insert the returned HTML into. However, that only makes sense if you're returning a partial, i.e. PartialView
. Returning View
means you're going to get the full HTML document, complete with all your layout, head, etc.
Upvotes: 4