I am using JSON and jQuery to make an asynchronous post back to controller.
Code in view:
@model MvcApplication3.Models.Class1
@{
ViewBag.Title = "Index";
}
<script src="~/Scripts/jquery-1.7.1.js"></script>
<script type="text/javascript">
jQuery(document).ready(function ($)
{
$('.remove').click(function ()
{
alert('Buton Clicked');
$.ajax({
type: "POST",
url: "/Default1Controller/RemoveItem/",
contentType: "application/json; charset=utf-8",
data: "{}",
dataType: "json",
success: removeItemCompleted,
error: removeItemFailed
});
});
function removeItemCompleted(results)
{
alert('SUCCESS');
}
function removeItemFailed(request, status, error)
{
alert('failed');
}
});
</script>
<h2>Index</h2>
<input id="Text1" type="text" />
<input id="Text2" type="text" />
<input id="Text3" type="text" />
<input id="Submit1" type="submit" value="submit" class="remove" />
Code in controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication3.Controllers
{
public class Default1Controller : Controller
{
//
// GET: /Default1/
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult RemoveItem()
{
return Json(new { Status = 1, Message = "Success" });
}
}
}
When the HTML button is clicked, the controller event is not getting called.
Anyone know the issue in above code?
$.ajax({
type: "POST",
url: @(Url.Action("RemoveItem", "Default1")),
contentType: "application/json; charset=utf-8",
data: "{}",
dataType: "json",
success: removeItemCompleted,
error: removeItemFailed
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.