我在一個頁面中有兩種形式。其中一種是添加評論,另一種是以下主要博客內容。如何防止Ajax重定向到Asp.net中的另一個頁面Mvc 5
我的目標是讓博客頁面添加評論功能。當用戶添加評論時,我想使用ajax發佈評論另一個控制器。當提供此功能時,我希望用戶留在同一頁面並更新評論部分在頁面上查看。但是,我無法通過下面的代碼實現此目的。
主要的博客內容
@model WforViolation.Models.Violation
@{
ViewBag.Title = "Details";
}
<h1>@Model.Title</h1>
<p class="lead">
Posted by @Model.CreatorUser.UserName<a href="#"></a>
</p>
<hr>
<!-- Date/Time -->
<p><span class="glyphicon glyphicon-time"></span> Posted on @Model.CreationDateTime <span class="glyphicon glyphicon-flag"></span> @Model.Severity Severity <span class="glyphicon glyphicon-comment"> </span> @Model.Comments.Count Comments </p>
<p><span class="glyphicon glyphicon-eye-open"> People Viewed</span></p>
<hr>
<!-- Preview Image -->
<img class="img-responsive" src="@Model.Picture.FirstOrDefault().MediumPath" alt="">
<hr>
<!-- Post Content -->
<p class="lead">@Model.Title</p>
<p>@Model.Description</p>
<hr>
<!-- Blog Comments -->
<!-- Comments Form -->
@Html.Partial("_AddComment", new WforViolation.Models.CommentViewModel() { ViolationId=Model.Id,Content=""})
<hr>
<!-- Posted Comments -->
<!-- Comment -->
@Html.Partial("_Comments",Model)
添加評論查看
@model WforViolation.Models.CommentViewModel
<div class="well">
<h4>Leave a Comment:</h4>
@using (Ajax.BeginForm("AddComment", "Comment", null, new AjaxOptions
{
HttpMethod = "POST",
OnSuccess = "SuccessMessage",
OnFailure = "FailMessage"
//UpdateTargetId = "resultTarget"
}, new { id = "MyNewNameId", role = "form" })) // set new Id name for Form
{
@Html.AntiForgeryToken()
<div class="form-group">
@Html.TextAreaFor(model => model.Content, new { @rows = 3 })
</div>
<button type="submit" class="btn btn-primary">Submit</button>
@Html.HiddenFor(model=>model.ViolationId)
}
</div>
<script>
//\\\\\\\ JS retrun message SucccessPost or FailPost
function SuccessMessage() {
alert("Succcess Post");
}
function FailMessage() {
alert("Fail Post");
}
</script>
我的評論控制器
[HttpPost]
public ActionResult AddComment(CommentViewModel commentViewModel)
{
Comment comment = new Comment();
var userID = User.Identity.GetUserId();
if (!string.IsNullOrEmpty(userID))
{
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
var currentUser = manager.FindById(User.Identity.GetUserId());
comment.ApplicationUser=currentUser;
}
var violationId = commentViewModel.ViolationId;
Violation violationCommentedOn = context.Violations.Where(x => x.Id == violationId).FirstOrDefault();
comment.Violation = violationCommentedOn;
context.Comments.Add(comment);
context.SaveChanges();
var result = Json(
new
{
Success = true,//success
StatusMessage = "Object created successfully"
});
return result;
}
當我添加一條評論時,它將我重定向到另一個名爲localhost/Comment/AddComment的頁面,它的內容就是Json。 是否有可能只是檢索JsonResult並留在同一頁面?
或者我不能使用ajax發佈表單數據而不是重定向到另一個頁面?
我知道有類似的問題,但我找不到確切的答案。 任何幫助表示讚賞。
我明白了,非常感謝! – erhan355
這個答案不正確。如果在視圖中包含了相關的'jquery-unobtrusive-ajax.js'文件,使用'Ajax.BeginForm()'將保留在同一頁面上。如果你不包含腳本,那麼'Ajax.BeginForm()'作爲'Html.BeginForm()',這就是你重定向的原因。 –
@Stephen添加jquery-unobstrusive-ajax。js並沒有改變任何東西都不起作用,因爲那樣它就會出現在用戶面前,就好像在提交時沒有任何事情發生一樣。控制器調用的返回結果需要顯示,可以重定向到相同的視圖或調用ajax並顯示返回結果。 –