using Ajax for partial view loading in .NET MVC4

前端 未结 7 1229
臣服心动
臣服心动 2020-12-17 15:01

I\'m trying to follow the post here, which may very well be wrong, to learn a little more about partial view loading in MVC. I know the basics of MVC but want to do more of

相关标签:
7条回答
  • 2020-12-17 15:34

    I typically load partials like this using JQuery

    $( document ).ready(function() {
    
      $("#lnkPage1").click(function( event ) {
    
        $('#mainDiv').load('Test/Pag1');
    
      });
    
      $("#lnkPage2").click(function( event ) {
    
        $('#mainDiv').load('Test/Pag2');
    
      });
    
    });
    
    0 讨论(0)
  • 2020-12-17 15:35
    @using (Ajax.BeginForm("AjaxViewCall", "Home", new AjaxOptions { UpdateTargetId = "result" }))   
    {
        <p>
            <input type="submit" value="Ajax Form Action" />
        </p>
    }
    
    <div id="result></div>
    

    On the button click it should call the Action that returns a PartialView. You dont need the '#' in your Ajax.BeginForm call.

    0 讨论(0)
  • 2020-12-17 15:38

    I copied your code into a brand new MVC4 project and it works fine.

    See screenshot of the solution:

    Brand new MVC4 project with Ajax.ActionLink

    Hope this will give you a hint on what could be wrong.

    0 讨论(0)
  • load json data or table partial view. not full format view.

    success:function(data){
        alert(data);
        debugger;
        ('#div_id').data(data);
    }
    

    here, returned data is json data or data like

    data

    . use alert to check the data.

    0 讨论(0)
  • 2020-12-17 15:40

    I got this exact same issue; loading the partial view as the whole page rather than in the defined div.

    I was incorrectly presuming that my _Layout.cshtml was including the jquery.unobtrusive-ajax.js file as I didn't understand what the Bundles thingy(?) was doing. I thought it was being added by Bundles.

    In my _Layout.cshtml View it defines @RenderSection("scripts", required: false) Which in English is; it's optional to include a section in your view called @scripts. So no error is thrown if you don't include it (@section scripts) but the symptom is that the Ajax just renders in a new page if you don't.

    To fix it: put the following in your view (I put it below the ...ViewBag.Title; })

    @section scripts{
        <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
    }
    

    Sorry if my explanation is a bit rudimentary, I'm a bit new to this. Hoping this helps someone.

    0 讨论(0)
  • 2020-12-17 15:44

    Only issue I had with original example was the \lib\ in script src path. Default MVC 4 Web app will put js files in "Scripts", not "Scripts\Lib". Your screen shot example is correct.

    <script src="@Url.Content("~/Scripts/jquery-1.8.2.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" 
    

    Also in the original example - putting in ViewBag.Title will cause this to not work:

    @{
        ViewBag.Title = "Some Title";
    }
    
    0 讨论(0)
提交回复
热议问题