Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

212
Visualizações
Passing value from view to controller using location.href is null

I have a view, in which I have a dropdown onchange event. I want to pass the selected value from the dropdown to the controller using location.href, but I can't manage to successfully do it.

I added in the alert to help me see what the value is, which correctly displays the selected value from the drop-down.

My view:

<div class="modelDropdown">
    @Html.DropDownListFor(m => m.groupId, Model.pointGroupDropdown, "Select a Model", new { @id = "pointGroupDropdown", @onchange = "LoadPointGroups(this)", @class = "form-control", @style = "width: 200px;" })
</div>

<script>
    function LoadPointGroups(pointGroup) {
        alert(pointGroup.options[pointGroup.selectedIndex].text);
        var groupName = pointGroup.options[pointGroup.selectedIndex].text;
        location.href = '@Url.Action("LoadPointGroups", new {groupName = "groupName"})';
    }
</script>

My controller:

public ActionResult LoadPointGroups(string groupName){
    *do stuff*
}

This results in groupName being "groupName" in the controller parameters.

I also tried

location.href = '@Url.Action("LoadPointGroups")/' + groupName;

But that passes null to the controller.

What is the correct way/syntax to do this? Any help is greatly appreciated!

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You could use Ajax request for this call. Here you have an example how to implement it :

var BaseUrl = '@Url.Content("~/")';

function LoadPointGroups(pointGroup) {
        var groupName = pointGroup.options[pointGroup.selectedIndex].text;  

 $.ajax({
              BaseUrl + yourControllerPath + '/LoadPointGroups?groupName=' + groupName,
              method: 'GET',  //Your controller's method type
              cache: false,
            }).done(function (whatYouGet) {
                // You could also do sth here if it's a get method
       }); 
   }
about 4 years ago · Juan Pablo Isaza Relatório

0

change routes configuration

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Help", action = "Index", id = UrlParameter.Optional }
            ).DataTokens = new RouteValueDictionary(new { area = "HelpPage" });
about 4 years ago · Juan Pablo Isaza Relatório

0

You can try this way:

function LoadPointGroups(pointGroup){
   alert(pointGroup.options[pointGroup.selectedIndex].text);
   var url =@Url.Action("YourMethodName", "ControllerName"); //your url
   var gName = pointGroup.options[pointGroup.selectedIndex].text; //your value
   window.location.href = url + "?groupName=" + gName;
 }
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda