Reputation: 1664
im trying to do some operation .Net Core and after this operation is done, i want to redirect it to a .cshtml page. In homepage i have table, after selecting a row in the table, im sending the value of the cell with ajax.
$('#table').find('tr').click(function () {
var userName = $(this).find('td').text();
$.ajax({
url: "/Profile/printUser",
type: 'POST',
data: { "DisplayName": userName }
});
});
After this part, im going to this area
[HttpPost]
public IActionResult printUser(User user)
{
user.DisplayName = user.DisplayName.Replace("\n", String.Empty);
user.DisplayName = user.DisplayName.Trim(' ');
User findUser = UserAdapter.GetUserByUserName(user.DisplayName);
return RedirectToAction("ProfileScreen",findUser);
}
My operations are finished, i found my user. All i want to do is print this users information in cshtml. But i cant send myselft to the page. How can i redirect myself? Thanks.
public IActionResult ProfileScreen()
{
return View();
}
Upvotes: 1
Views: 2847
Reputation: 1664
SOLUTION
[HttpPost]
public JsonResult printUser(User user)
{
user.DisplayName = user.DisplayName.Replace("\n", String.Empty);
user.DisplayName = user.DisplayName.Trim(' ');
User findUser = UserAdapter.GetUserByUserName(user.DisplayName);
return Json(new { displayName = findUser.DisplayName});
}
Upvotes: 0
Reputation: 8022
You can't redirect from Ajax call in the backend. Use AJAX's
success: function(){
windows.location.href = '/ProfileScreen';
}
If you want to pass data back, return JSON from MVC action and your JavaScript would be:
$('#table').find('tr').click(function () {
var userName = $(this).find('td').text();
$.ajax({
url: "/Profile/printUser",
type: 'POST',
data: { "DisplayName": userName },
success: function(data){
window.location.href = '/ProfileScreen' + data.ID; //or whatever
}
});
});
Upvotes: 1