i want run JavaScript/jQuery from view but didn't show me anything. my code work and data is recorded in database but don't know why JavaScript/jQuery didn't work
my code are:
Action Code:
[HttpPost]
public ActionResult Register(User user)
{
UserRepository blUser = new UserRepository();
if (ModelState.IsValid)
{
if (blUser.Add(user))
{
return Json(new JsonData() { Success = true });// return {"Success":true}
}
else
{
return Json(new JsonData() { Success = false }); // return {"Success":false}
}
}
else
{
return Json(new JsonData() { Success = false }); // return {"Success":false}
}
}
and view code:
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
<script>
$(document).ready(function (e) {
e.preventDefault();
$("#reg-btn").click(function () {
$.ajax({
type: "Post",
url: "/Home/Register",
data: null,
dataTpe: "Json",
success: function (result) {
if (result.Success == true) {
alert("success");
}
},
error: function () {
alert("error");
}
});
});
});
</script>}
nothing happen when submit button pressed just data record in database ... just in new page show me " {"Success":true} " if i removed this line code in view:
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
still didn't work if i changed return in controller
return JavaScript("alert('success')"); // again show string "alert('success')" in new page if above line code removed
and partial view too...
which JavaScript reference file added to my project? default created by visual studio 2015 / MVC 4 + jquery.unobtrusive-ajax.min.js
ty
e.preventDefault()
should be inside the click event handler.