Archive for the ‘ASP NET MVC’ tag
Ajax post requests in Asp Net Mvc
I really enjoy that Asp Net Mvc gave me back the control of my Html.
Now I can easily work on nice javascript user interactions and integrate it with server side code in a very clean approach.
So let’s see how I am doing Ajax post requests in Asp Net Mvc, on the top of jQuery library.
At the master page I have a hidden input called ErrorAction, so my client-side code will know what url should be called when an error occurs in Ajax requests.
Still at the master page, we tell jQuery that by default this Error Action should be invoked when something bad occurs while executing a request.
Finally, we can work on the post requests in a safe way. And that is really easy.
Here is how it works.
<% =Html.Hidden("ErrorAction", Url.Action("Index", "Error"))%>
<script type="text/javascript">
jQuery.ajaxSetup({
error: function(XMLHttpRequest, textStatus, errorThrown)
{ window.location = $("ErrorAction").value; }
});
</script>
<% =Html.ActionLink("Update Permissions", "Permissions", null ,
new { id = "updatePermissions",
style = "visibility: hidden;",
onclick = @"jQuery.ajax(
{
url:this.href,
type:'POST',
data:
{
scrumMasters:permissionsInRole('scrumMasters'),
productOwners:permissionsInRole('productOwners'),
teamMembers:permissionsInRole('teamMembers'),
chickens:permissionsInRole('chickens')
},
complete: permissionsUpdated
}); return false;"
})%>
(Portuguese) Asp Net MVC: T4 te dá asas e produtividade
Sorry, this entry is only available in Portuguese.
(Portuguese) ASP NET MVC 1.0 lançado oficialmente
Sorry, this entry is only available in Portuguese.