0

I am doing a web application using ASP.NET MVC 5. It seems to me that I did everything as it goes, but ajax is just not working. I see results only after refreshing. This is in my View:

@{
    AjaxOptions ajaxOptions = new AjaxOptions { 
        UpdateTargetId = "CommmentList", 
        HttpMethod = "Post", 
        InsertionMode = InsertionMode.Replace};

}
<div id="CommentList">
    @using (Ajax.BeginForm("Index", "Comment", ajaxOptions))
{
// some content
<div>
    @Html.Action("_AddCommentForItem", "Comment")
</div>
}
</div>

This is in a layout view:

<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.8.0.min.js" type="text/javascript"></script>
<script src="http://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.unobtrusive-ajax.js" type="text/javascript"></script>

EDIT:

These are actions in a Comment controller:

 // GET: Comment
    public async Task<ActionResult> Index()
    {
        var comments = db.Comments.Include(k => k.Item);
        return View(await comments.ToListAsync());
    }

// GET
    public PartialViewResult _AddCommentForItem()
    {
        ViewBag.ItemId = new SelectList(db.Items, "Id", "Name");

        return PartialView("_AddCommentForItem");
    }


    // POST
    [HttpPost]
    public PartialViewResult _AddCommentForItem ([Bind(Include = "Id,ItemId,CommentContent")] Comment comment)
    {

        if (ModelState.IsValid)
        {
            db.Comments.Add(commment);
            db.SaveChanges();

        }

        ViewBag.ItemId = new SelectList(db.Items, "Id", "Name", comment.ItemId);
        return PartialView("_AddCommentForItem");
    }
  }
}

I included partial view _AddCommentForItem, which is creating a comment, in an index view of the Comment controller. That's way i want the result to be visible right away, without refreshing.

What am I missing? Thank you.

SiSharp
  • 121
  • 1
  • 8
  • 1
    What do You mean by doesn't work. Some exception thrown? the form is not replacing or what? – szpic Sep 06 '14 at 20:45
  • Yes, the form is not replacing. It acts like normal post method, and results show after refreshing. – SiSharp Sep 06 '14 at 21:09
  • You have two actions `Comment/Index` and `Comment` -- Is that right? What does their code look like? – Jasen Sep 06 '14 at 21:19
  • based on [this](http://msdn.microsoft.com/en-us/library/dd505270(v=vs.118).aspx) you are setting `index` as an action and `Comment`as controler are You sure that it is correct? – szpic Sep 06 '14 at 21:23
  • 1
    Check the browser console for errors. The version of `jquery.unobtrusive-ajax` you are using is outdated and references `jquery-1.4.4.js` (for example it uses `.live()` which was depreciated in jquery version 1.7 and may be causing the problem. Try using the latest non-minified versions of both files. –  Sep 07 '14 at 05:35
  • Index is an action of the Comment controller. I edited the post to provide information you mentioned. – SiSharp Sep 07 '14 at 15:42
  • I also added new versions of references: `` `` The problem is still the same, so I don't think that's it. – SiSharp Sep 07 '14 at 16:15
  • Check your scripts again -- Read [this](http://stackoverflow.com/questions/23895918/mvc5-ajax-beginform-and-the-correct-scripts-load-order/23897170#23897170). Next, you are setting your form to post to `Comment/Index` yet you do not have a matching `POST Comment/Index` action. I suggest you remove the partials until you can confirm you are able to AJAX post a simple model to your controller. – Jasen Sep 08 '14 at 19:04

0 Answers0