1

Based on which button is clicked i want to render a partial View inside a div element. If button1 is clicked then Partial View1 is rendered and if button2 is clicked then Partial View2 is rendered. How can i do it.

Main View:enter code here

<button id="button1">One</button>
<button id="button2">Two</button>

<div id="search">
//Render Partial view here based on which button is clicked.
</div>

Partial View1:

<p>In Partial View1</p>

Partial View2:

<p>In Partial View2</p>

   


After i click the blue button, i want partial page to be rendered in Div area below the blue button. But its rendering above the button in grey area which is my common layout area.

Before click enter image description here

After enter image description here


I tried this code but the problem is that partial view is loading but not within the div element.

        <input id="btnSearch" type="button" class="btn btn-primary" value="Search" />
        
        <div id="students">
           
        </div>

<script>
    $('#btnSearch').on('click',
        function (e) {
            $.ajax({
                url: '/Home/About',
                    type: 'POST',
                    cache: false,
                    async: true,
                dataType: "html"
                

                })
                .done(function(result) {
                    $('#students').html(result);
                }).fail(function(xhr) {
                    console.log('error : ' + xhr.status + ' - ' + xhr.statusText + ' - ' + xhr.responseText);
                });

        });

</script>

2
  • You have to work with client side library like jQuery or pure javascript and load html of view by ajax call and placeholder like div on the page. Look at similar answer stackoverflow.com/questions/56374272/… Commented Sep 25, 2019 at 3:30
  • Thanks Oleg i tried below but the problem is that partial view is loading but not within the div element. Commented Sep 25, 2019 at 5:31

1 Answer 1

1

I think the code you posted should work, but you need to query the right endpoint of your home controller. Given your minimal modified code (see the ajax url):

        <input id="btnSearch" type="button" class="btn btn-primary" value="Search" />

        <div id="students">

        </div>

<script>
    $('#btnSearch').on('click',
        function (e) {
            $.ajax({
                url: '/Home/View2',
                    type: 'POST',
                    cache: false,
                    async: true,
                dataType: "html"


                })
                .done(function(result) {
                    $('#students').html(result);
                }).fail(function(xhr) {
                    console.log('error : ' + xhr.status + ' - ' + xhr.statusText + ' - ' + xhr.responseText);
                });

        });

</script>

And your HomeController.cs should have such a second method:

public IActionResult View2(){
  return PartialView();
}

Edit I guess these pages could help you to get a rough idea of controllers and the handling:

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.