Ajax.BeginForm refreshing the whole page in MVC

落爺英雄遲暮 提交于 2019-12-02 03:07:51

I use an ajax call in jquery for this

$('#SelectedFeedOption').change(function() {
    $.ajax({
        url: "@(Url.Action("Action", "Controller"))",
        type: "POST",
        cache: false,
        async: true,
        data: { data: $('#SelectedFeedOption').val() },
        success: function (result) {
            $(".feedList").html(result);
        }
   });
});

Then put the contents of your feedList div in a partial view and on your controller

public PartialViewResult FeedList(string data){
    Model model = (get search result);
    return PartialView("_feedList", model);
}

Hopefully this helps.

Niko

Did you try initializing the InsertionMode member into the AjaxOptions object initializer?

You also have to include 'jquery.unobtrusive-ajax.js' to make Ajax.BeginForm to work as answered here

 @using (Ajax.BeginForm("Index", "Home", null,
    new AjaxOptions
    {
        HttpMethod = "post",
        InsertionMode = InsertionMode.Replace,            
        UpdateTargetId = "feedList"
    });
{
    @Html.DropDownListFor(x => x.SelectedFeedOption, Model.FeedOptions)
    <input type="submit" value="Submit" /> 
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!