Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh only partial view in MVC4

I'm having a little bit of trouble with refreshing a partialview on mvc4, here is the code:

    <div id="partialViewDiv"></div>

    <input type="button" id="firstPartialLink" value="Change Div"/>
    <input type="button" id="secondPartialLink" value="Change Div"/>

    <script type="text/javascript">
        $(function() {
            $("#firstPartialLink").click(function() {

                $("#partialViewDiv").load('@Url.Action("GetDiv", "Home")');

            });

            $("#secondPartialLink").click(function () {

                $("#partialViewDiv").load('@Url.Action("GetDiv2", "Home")');
            });
        })

    </script>

When I press one of the buttons the first time, it renders the partialview inside the DIV, but when I press it again, nothing happens, what would be the cause?

like image 901
Victor Franchi Zeclhynscki Avatar asked Jul 21 '13 21:07

Victor Franchi Zeclhynscki


People also ask

How to display a partial view?

To create a partial view, right click on the Shared folder -> click Add -> click View.. to open the Add View popup, as shown below. You can create a partial view in any View folder. However, it is recommended to create all your partial views in the Shared folder so that they can be used in multiple views.

How to render partial view in. net Core MVC?

In ASP.NET Core MVC, a controller's ViewResult is capable of returning either a view or a partial view. In Razor Pages, a PageModel can return a partial view represented as a PartialViewResult object. Referencing and rendering partial views is described in the Reference a partial view section.

How to call the partial view in MVC?

To create a partial view, right-click on view -> shared folder and select Add -> View option. In this way we can add a partial view. It is not mandatory to create a partial view in a shared folder but a partial view is mostly used as a reusable component, it is a good practice to put it in the "shared" folder.


1 Answers

try to use this delegate to handle click event

            $(document).on("click","#firstPartialLink",function() {

                $("#partialViewDiv").load('/Home/GetDiv',function(html) { if need it});

            });
like image 54
Timur Shahbanov Avatar answered Sep 21 '22 11:09

Timur Shahbanov