Ajax.BeginForm refreshing the whole page in MVC

后端 未结 2 437
余生分开走
余生分开走 2021-01-24 17:26

I\'ve been trying to add some Ajax functionality to my mvc site, however, I run into a problem regarding page refreshing. I\'ve created an rss view on my homepage sidebar, which

相关标签:
2条回答
  • 2021-01-24 17:43

    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.

    0 讨论(0)
  • 2021-01-24 18:07

    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" /> 
    }
    
    0 讨论(0)
提交回复
热议问题