Footer is repeating when I press Partialview in the dropdown change

Asked

Viewed 46 times

0

I changed the question to make it clearer, it was dirty and now I understand the problem better. When I open the list, I have a Details button. When I click on this button, I open the details screen and on this screen a Dropdownlist. It happens that when I click on Dropdown, a jquery is triggered that should repopulate the grid, already with the new id and etc. This repopulation is a Partialview that should be loaded and is not being, only the complete view and so the Footer is doubling. I cannot identify in Controller when the call is from View and when it is from Jquery. If I could do this, in the controller I would be able to return to View or Partialview("name"). I will post Controller and jquery.

[HttpGet]
        public ActionResult Details(AzureDiscountGroupModel model, int id)
        {
            ViewBag.Indice = id;
            var discount = _azureDiscountGroupService.GetAll();
            var list = new List<ResellerListModel>();

            var resellers = _resellerService.QueryAll()
                .Include(r => r.WhiteLabels)
                .ToList();

            foreach(var item in resellers)
            {
                list.Add(CreateListModelFrom(item));
            }

            ViewBag.Desconto = discount.Where(x => x.Id > 0);

            ViewBag.DetailReseller = list.Where(x => x.AzureDiscountGroupId == id).ToList();

           //aqui deveria fazer um if para uma chamada ou outra
            return PartialView();
        }

This is my jquery

$(document).ready(function () {
        $("#GrupoDescontos").change(function () {
            var $div = $('#GridPartial');
            $div.html('');
            $.ajax
            ({
                url: '' + $(this).val(),
                type: 'GET',
                    success: function (dados) {
                        $div.html(dados);
                },
                    error: function (erro) {
                }
            });
        });
    });

How do I identify a View call or if it’s from Jquery?

  • The following, I can resolve by passing a parameter from the View Index call. Well, that works, but Partialview is just a table and when I call from jquery, there is no more header and dropdown. If I put these details in Partialview, in the first load coming from Index, then everything is duplicated.

  • I solved in part. I step one parameter and remove all the View. I left only the div where the partialview would be populated. This has solved itself, the problem is that I can not make more than one call by Dropdownlist. I do an ok, but from there it does not run more

  • When you ask about a problem in your code, you’ll get better answers if you give people code that they can use to reproduce the problem. See how to create a minimum, complete and verifiable example to use in your question.

2 answers

0

make sure that your Partialview is not loading the layout of the master.. which may be coming from _ViewStart

@{
    Layout = null;
} 
<table class="table">
    <tr>
        <th>
            Nome
        </th>
        <th>
            Alias
        </th>
        <th>
            WhiteLabel?
        </th>
        <th>
            MPN Id
        ...

And simplify your code, if you’re not handling the error and just doing a GET to get the rendered content, use the method load()

<script type="text/javascript">
    $(document).ready(function () {    
       $("#GrupoDescontos").change(() => {$('#GridPartial').load('' + $(this).val());});                
       $("#GrupoDescontos").val("@ViewBag.Indice");
    });
</script>
  • What I could deduce was this: return PartialView(). When the page goes up the first time ok, but the second time should be just that return PartialView("DetailsPartial"), then yes, only the table would be loaded by ajax and not every page, but how would you do that? Or do an Action just for that repeating the same code except in Return, it sounds kind of ugly.

  • Lenadr, the method of load() did not work. When I make the second change, from there nothing else happens

  • nothing else happens where?

0

I decided as follows In the view I left only the div where would be placed the Partialview. I pass an Index parameter to the Controller and tell if it is the first pass. Then I return to View or Partialview

Browser other questions tagged

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