I am trying to call the following action method in a controller from a javascript function in a partial view:
[HttpPost]
public JsonResult RemoveNotifications(IList<Notification> notifications)
{
Context.Notifications.RemoveRange(notifications);
return new JsonResult();
}
This is the function that should call the server from the view:
function show(message) {
message += "</ul>"
document.getElementById('notifications').innerHTML = message;
$.ajax({
url: '@Url.Action("RemoveNotifications", "Notification")',
type: 'POST',
data: { 'notifications': "@Model.Notifications" },
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: function (data) {
}
});
}
The first part works fine, but the $.ajax part dosen't do anything. I also have the following added at the top of my view:
<script src="TandForum.dk/Scripts/jquery-1.9.1.min.js" type="text/javascript"></script>
<script src="TandForum.dk/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="TandForum.dk/Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
@Model.Notifications
will return a string (something like"System.Collections.Generic.IList<Notification>"
, not the collection. What is the point of this. You sent the collection to the view from the controller so you already know what it is. There is no reason to send it from the view to the controller, just get the same collection again in theRemoveNotifications()
method – Stephen Muecke Apr 19 at 22:33