Reload page after jQuery.get to MVC controller action
If you need to update the entire page, you don't need to use the $.get
, an ajax method.
Try this instead:
window.location = '/payments/UpdatePayments?json=' + JSON.stringify(payments);
jQuery Reload current page after return from MVC Controller Action called from jQuery
If you're going to reload the page after a bit of AJAX returns, there's no point in using AJAX. Just do a normal GET or POST.
That said, your problem here is that AJAX is asynchronous. In other words, after the line $('#test').load(...)
is executed, the next line is immediately executed, before the whole AJAX bit has had a chance to send the request and receive the response. In order to execute code only after the response has returned, you have to pass a callback:
$('#test').load('/Account/TestPartial/' + culture, function () {
// do something
});
MVC refreshing page after Ajax call to controller
The solution was to change the ajax call success part to:
success: function (data) {
$("body").html(data);
}
Jquery Select load back after refresh page
Your problem in your country options it is been selected first option automatically while you refresh,
so you can set your desire country for the states in page loading.
<script>
$(document).ready(function () {
**$('#CountryId').val('desire country');**
$("#CountryId").change(function () {
$.get("@Url.Action("GetStateList","Controllers1")", { sOId: $("#CountryId").val() }, function (data) {
$("#StateId").empty();
$.each(data, function (index, row) {
$("#StateId").append("<option value='" + row.PersonnelNumber + "'>" + row.Name + "</option>")
});
});
})
});
</script>
Refreshing Page After Delete With Ajax in ASP.NET MVC
You are trying to redirect from Controller
which is not really best done using AJAX
. AJAX
is used to update a portion of the page and is generally not used to redirect to some other page. In your case you can do the following:
You need to return Json
from your Controller
method. You can do that by defining your method to be JsonResult
which will return Json
to your AJAX
call.
You can also use ActionResult
if there are multiple return types possible deriving from ActionResult
, like ViewResult
, FileResult
, JsonResult
, etc.
Since you are doing a GET
action and MVC defaults to DenyGet
to protect you against a very specific attack involving JSON requests, you need to allow it explicitly. If it were a POST
action, then you do not need to bother with setting the JsonRequestBehavior
.
[HttpGet]
public JsonResult Delete(int id)
{
int rowCount=0;
using (var connection =
new SqlConnection("Server=(localdb)\\MSSQLLocalDB;Integrated Security=true;Initial Catalog=Todo"))
{
using (var tableCmd = connection.CreateCommand())
{
connection.Open();
tableCmd.CommandText = $"DELETE from todo WHERE Id = '{id}'";
rowCount = tableCmd.ExecuteNonQuery();
}
}
if(rowCount > 0)
{
return Json(new {status="true", msg= "Successfully deleted"}, JsonRequestBehavior.AllowGet);
}
else
{
return Json(new {status="false", msg= "Could not delete"}, JsonRequestBehavior.AllowGet);
}
}
And you can handle this in your AJAX
like this:
function deleteTodo(i)
{
$.ajax({
url: 'Todo/Delete',
type: 'GET',
dataType: 'json',
data: {
id: i
},
success: function(data) {
if(data.status=="true")
{
var urlToRedirect= '@Url.Action("Index","Home")';
window.location.href = urlToRedirect; //Redirect here
}
else if(data.status=="false")
{
alert(data.msg)
}
},
error: function() {
alert('fail');
window.location.reload();
}
});
}
Jump/Reload to same page from Ajax success function on razor view in MVC
Please try by removing the dataType from the ajax call.
$.ajax({
url: "/api/AcceptBooking/AcceptBooking",
method: "PUT",
data: Booking,
success: function (response) {
window.location.href = url_redirect;
}
});
AJAX - Post to MVC Controller method without reloading page
The page reload was occurring because the button exists within an HTML Form. By default, the "type" attribute of HTML buttons is "submit" unless otherwise specified. Since I hadn't specified a "type" for my button, clicking the button was being interpreted as as a form submit.
To resolve this, I set the type attribute for my button as "button"
<button type="button" id="btnListGroups" title="List the groups the selected service ID is a member of">List Groups</button>
Related Topics
How to Save/Download Pdf Embedded in Web Page Without a Pdf Filename
How to Secure the Asp.Net_Sessionid Cookie
Returning a File to View/Download in ASP.NET MVC
Deserialize Json in C# - How to Handle Null Return Values
How to Correctly Write Parallel.For With Async Methods
C# Download Big File from Server With Less Memory Consumption
Convert Date Format of String to Datetime Mm/Dd/Yyyy When System Date Formate Is Dd/Mm/Yyyy
C# - How to Loop Through a Table to Update Each Row MySQL
Linq to SQL Left Outer Join Using Lambda Syntax and Joining on 2 Columns (Composite Join Key)
Reading from Excel File Using Closedxml
How to Reference Assemblies Using Visual Studio Code
Automatic Enhancement of Scanned Images
How to Ignore First Two Columns of CSV File
Validate Indian Phone Number With Optional +91 or 0 Preceeding 10 Digits
How to Refresh an Entity Framework Core Dbcontext