How to call controller method for a View

Asked

Viewed 345 times

0

Nessa Dropdown

<div class="form-group">
        @Html.Label("Grupo de Desconto", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("Grupo de Descontos", new SelectList(ViewBag.Desconto, "Id", "Descricao"))
            @*@Html.ValidationMessageFor(model => model.HomeTemplateId, "", new { @class = "text-danger" })*@
        </div>
    </div>

As I do when selecting an item, it calls a method in the controller to filter a table(grid) by passing the ID in the Dropdown?

EDIT1

Well, I remade and I can with jquery get to controller. Change the Viewbag that populates the Grid(Table), but the grid does not reload. This is the new Action

[HttpGet]
        public ActionResult Details(AzureDiscountGroupModel modelD, int 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();
            return View(modelD);
        }

I changed jquery to that

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

        }
     });
  });
});

Everything seems to be ok, but the grid does not update. This is the cshtml of the project

@model Atma.SND.CSP.WhiteLabel.Site.Areas.API.Models.AzureDiscountGroups.AzureDiscountGroupModel
@using Atma.SND.CSP.SubscriptionCenter.API.Models.Resellers

@{
    ViewBag.Title = "Details";
}

<style>
    .Makewide { width: 200px; }
</style>

<h2>Details</h2>

<div>
    <h4>AzureDiscountGroupModel</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.HiddenFor(model => model.Id)

    <div class="form-group">
        @Html.Label("Grupo de Desconto", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("GrupoDescontos", new SelectList(ViewBag.Desconto, "Id", "Descricao"), new { @id = "GrupoDescontos", @class= "Makewide" })
            @*@Html.ValidationMessageFor(model => model.HomeTemplateId, "", new { @class = "text-danger" })*@
        </div>
    </div>

</div>

<table class="table">
    <tr>
        <th>
            Nome
        </th>
        <th>
            Alias
        </th>
        <th>
            WhiteLabel?
        </th>
        <th>
            MPN Id
        </th>
        <th>
            Criada em
        </th>
    </tr>

    @foreach (var item in ViewBag.DetailReseller as List<ResellerListModel>)
    {
    <tr align="center">
        <td>
            @(item.Name)
        </td>
        <td>
            @(item.Alias)
        </td>
        <td>
            @(item.WhiteLabel ? "Sim" : "Não")
        </td>
        <td>
            @(item.MpnId)
        </td>
        <td>
            @(item.CreatedOn)
        </td>
    </tr>
    }

</table>

<p>   
    @Html.ActionLink("Back to List", "Index")
</p>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("#GrupoDescontos").change(function () {
        $.ajax
            ({
                url: '' + $(this).val(),
            type: 'GET', 
                success: function (dados) {

                    alert(1);
                var resultado = dados;
            },
            error: function (erro) {

            }
});
  });
});
</script>

The Partialview I created

<div id="GridDetail">
    <table class="table">
        <tr>
            <th>
                Nome
            </th>
            <th>
                Alias
            </th>
            <th>
                WhiteLabel?
            </th>
            <th>
                MPN Id
            </th>
            <th>
                Criada em
            </th>
        </tr>

        @foreach (var item in ViewBag.DetailReseller as List<ResellerListModel>)
        {
            <tr align="center">
                <td>
                    @(item.Name)
                </td>
                <td>
                    @(item.Alias)
                </td>
                <td>
                    @(item.WhiteLabel ? "Sim" : "Não")
                </td>
                <td>
                    @(item.MpnId)
                </td>
                <td>
                    @(item.CreatedOn)
                </td>
            </tr>
        }

    </table>

</div>
  • When I enter Details it already filters the table, I just wanted to know how I do the View to redo, that is, I select another Group and the ID of that group, already filter

  • This is an alternative https://answall.com/questions/304892/o-component-n%C3%A3o-existe-no-contexto-actual/304939#304939

  • I’m trying to get the dropdown value like this: $(this).val(); but it doesn’t work

  • You see, I was able to send the correct id to each Dropdown change, enter the controller, change the Viewbag that loads the grid, but does not display the changes in the grid. On the grid nothing happens

  • It’s about whether or not to be on the server?

  • Ajax... you need to make a request and update the content of your page

  • But how do I do it, @Leandroangelo? Trying for partial view and got nothing

  • Where is the element with this ID here? #Griddetail that I did not find in your HTML? It must be so.. in the case the Grid to which you refer is the Table?

  • The whole question is how to formulate the question, that’s the problem. Question badly asked, has no answer and I can’t be more direct

Show 4 more comments
No answers

Browser other questions tagged

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