View:
@{
AjaxOptions ajax = new AjaxOptions() { HttpMethod = "POST", UpdateTargetId = "sub_id" };
Layout = null;
}
<div id="sub_id"></div>
@using (Ajax.BeginForm(ajax))
{
@Html.TextBox("email");
<input type="submit" value="подписаться" />
}
controller:
[HttpPost]
public ContentResult LeftMenuSubscription(string email)
{
return new ContentResult(){Content = "<script>alert('Thanks')</script>"};
}
“Thanks” alert show.
but in div sub_id set all page(<title></title><div>...</div>).
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
– connected.
html:
<html>
<head>
<title>Главная страница</title>
<link href="/Content/Site.css" rel="stylesheet" type="text/css">
<script src="/Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"</script>
<script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
<script src="/Scripts/jQueryFixes.js" type="text/javascript"></script>
</head>
<body>
....
<form action="/" data-ajax="true" data-ajax-method="POST" data-ajax-mode="replace" data-ajax-update="#sub_id" id="form0" method="post">
<input id="email" name="email" type="text" value="">
<input type="submit" value="подписаться">
</form>
....
</body>
</html>
What could be the problem?
Looks to me like your Ajax options aren’t complete. I believe you need to add the Action that you are requesting from:
Running this in a test I get a popup alert, which is exactly what should happen because you are returning a script that says:
<script>alert('Thanks')</script>If you just want the div to show
Thanksthen don’t return the script tagUse this if you just want to have the word thanks show up in the div
This works perfect for me using the modified form