How I Address a Partialview ASP MVC

Asked

Viewed 78 times

1

I have a View who has a div and within it goes a partialview

EX:

<div>
     @Html.RenderPartial("MeuPartial");
</div>

In the right page render load, however, I needed it to render every time I click on a button.

<button class="btn btn-primary" [email protected]("MeuPartial");>Atualizar</button>

the onclick does not accept the method. How do I render inside the div?

2 answers

1

You can create an Action to return you to partialview, and use jQuery .load() to seek and popular its partial. It would look something like this:

Action:

public PartialViewResult GetPartial()
{
    return PartialView("~/Areas/Sua_area/Views/Shared/_MeuPartial");
}

And the part in Javascript:

$("#idSuaDiv").load('url_sua_action', function(res, status) {
  if (status == 'success') {
        //OK
  }
})

0


Recently I had to do exactly that and after so much research I decided to use javascript to do the job of carrying the partialview within the div.

Javascript

       function RefreshList() {
            var url = '@Url.Action("ListProduction", "Home")';
            $.post(
            url,
            function (data) {

                $('#listproduction').html(data);

            });

        }

HTML

<a href="javascript:RefreshList();" class="btn btn-primary"><i class="fa fa-refresh">&nbsp;Atualizar</i></a>
<div id="listproduction">                    
      @Html.Partial("ListProduction")
</div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.