In ASP.NET MVC 3 application I use Ajax.BeginForm to post writed text to controller.
@using (Ajax.BeginForm("Post", "Forum", new {threadId = Model.Thread.Id }, new AjaxOptions { OnSuccess = "PostReply" }))
{
<div id="reply-area">
<h3 style="border-bottom:1px solid black">POST REPLY</h3>
<span id="post-error" class="error-message"></span>
<textarea rows="1" cols="1" id="post-textarea" name="Content"> </textarea>
<input type="submit" class="button" value="Submit"/>
</div>
}
In controller I have
[HttpPost]
public ActionResult Post(int threadId,PostModel model)
{
bool Success = false;
if (ModelState.IsValid)
{
Success=Unit.ForumFacade.CreatePost(Unit.ForumFacade.GetThreadByID(threadId), model.Content, CurrentUserId);
if (Success == true) return View("PostSuccess");
}
return Json("fsdfds");
}
And in javascript I have this
function PostReply(isRequestSuccessed) {
alert("asdasd");
if (isRequestSuccessed==false) {
$("#post-error").html("Please Try Again");
}
else
{
$("#post-error").html("");
}
}
The problem is that Javascript's function doesn't firing and isntead the alert, my browser(Firefox) returns me pop-up to download application/json file. What is wrong here?
Make sure you have included the following script to your page:
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
and that you have enabled unobtrusive javascript in your web.config:
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
This is what makes Ajax.*
helpers such as Ajax.BeginForm
to work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With