Tuesday

asp.net mvc ajax post from javascript

Following example demonstrate how to submit form in asp.net MVC for strict type view based on model:
(view has this instruction oin top:
@model MyModel
):

javascript part:
      
<script type="text/javascript">
$(document).ready(function() {
$("#GoButton").click(function() {
var frm = $(this).closest('form');
var params = frm.serialize();
$.ajax({
type: 'POST',
url: '@Url.ApplicationAction("MyPostAction", "myController")',
data: params,
cache: false,
success: function (data) {
alert('Completed');
},
error: function (xhr, data, message) {
//console.log(data + ": " + message);
},
dataType: "json"
});
return false;
});
});
</script>


on server side model variable will have updated with all values submitted in form and will have following parameters:
 
[HttpPost]
public ActionResult MyPostAction(MyModel model)
{

return Json("Ok");
}




No comments:

test smtp server with powershell

Send-MailMessage -SMTPServer smtp.domain.com -To [email protected] -From [email protected] -Subject "This is a test email" -Body ...