Load partial view depending on dropdown selection in MVC3

后端 未结 2 1502
清酒与你
清酒与你 2020-12-08 11:24

Im trying to create a from using asp.net mvc3.

I have a dropdownlist with some options. What i want is different partial views to be injected into the page, dependi

相关标签:
2条回答
  • 2020-12-08 12:03

    add the following code to the header of the project (layout). Add "combobox" to any combo box (select box) which you want to trigger the form that is surrounding it.

    $(document).ready(function () {
        $('.formcombo').change(function () {
            /* submit the parent form */
            $(this).parents("form").submit();
        });
    });
    
    0 讨论(0)
  • 2020-12-08 12:15

    Let's say following is your view that you want to insert your partial.

    <html>
        <head><head>
        <body>
            <!-- Some stuff here. Dropdown and so on-->
            ....
    
            <!-- Place where you will insert your partial -->
            <div id="partialPlaceHolder" style="display:none;"> </div>
        </body>
    
    </html>
    

    On the change event of your dropdownlist, get partial via jquery ajax call and load it to place holder.

    /* This is change event for your dropdownlist */
    $('#myDropDown').change( function() {
    
         /* Get the selected value of dropdownlist */
         var selectedID = $(this).val();
    
         /* Request the partial view with .get request. */
         $.get('/Controller/MyAction/' + selectedID , function(data) {
    
             /* data is the pure html returned from action method, load it to your page */
             $('#partialPlaceHolder').html(data);
             /* little fade in effect */
             $('#partialPlaceHolder').fadeIn('fast');
         });
    
    });
    

    And in your controller action which is /Controller/MyActionin above jquery, return your partial view.

    //
    // GET: /Controller/MyAction/{id}
    
    public ActionResult MyAction(int id)
    {
       var partialViewModel = new PartialViewModel();
       // TODO: Populate the model (viewmodel) here using the id
    
       return PartialView("_MyPartial", partialViewModel );
    }
    
    0 讨论(0)
提交回复
热议问题