How to make an Event Change in ASP.NET Core MVC?

Asked

Viewed 332 times

0

In ASP.NET we have the Event Change right in the Code Be-hind

 protected void ddlCiclo_SelectedIndexChanged(object sender, EventArgs e)
    {
         //algo aqui
    }

What would be the equivalent in ASP.NET Core MVC for a DropDownList?

  • 3

    I think the best way is to add a Istener to the event change select, via Javascript, and make an AJAX call, or else give a Submit in the form

  • 3

    There is no MVC equivalent. There is no code Behind or postback. What you do is capture the JS event change of the associated HTML element and make a call GET (or PUT) to the controller.

1 answer

1


It is difficult to compare two so different paradigms: Web Forms x MVC.

Just to illustrate, see the treatment flow of an event in ASP.NET Webforms and ASP.NET MVC: inserir a descrição da imagem aqui Image source: Slide share: Mvc Presentation

In your specific case, you want to treat the event of modifying a value in a drop-down list.

The visual interface of ASP.NET MVC is composed of HTML + Javascript + CSS (lembando MVC is an architecture standard that can be applied outside the WEB scope)

In the case of a suspended list, you can do this: Imagine that you need a list with the user’s birth YEAR, and in the selection you need to do something 1 - First you need a model (MVC’s M)

public class AnosModel
{
    public string anoNasc{get;set;}

    public List<SelectListItem> listaAnos() // simular um repositório. Eu sei que isso deveria estar em outra camada, mas por motivos de simplificação eu coloquei aqui.
    {
       List<SelectListItem> anos= new List<SelectListItem>()
        {
            new SelectListItem { Text = "1970", Value = "1" },
            new SelectListItem { Text = "1971", Value = "2" },
            new SelectListItem { Text = "1972", Value = "3" },
            new SelectListItem { Text = "1973", Value = "4" },
            new SelectListItem { Text = "1974", Value = "5" },
            new SelectListItem { Text = "1975", Value = "6" }

        };
        return anos;
    }
 }

2 - We create the View (MVC V. In this example I will omit the complete creation of the view and focus on building the drop-down list)

    @Html.DropDownListFor(model => model.AnoNasc,  (IEnumerable<SelectListItem>)ViewBag.Anos, 
        new { onchange = "modificado(@Model.AnosNasc)" })

<script>
    function modificado(ano) {
        // fazer algo aqui. Você pode inclusive fazer uma chamada ao controller usando um dos verbos REST (GET, PUT, POST, DELETE, etc..)
        if (ano<"1974") {
          alert("Você é velho!");
       }
       else
       {
         alert("Você é jovem!");
       }
    }
</script>

3 - And the controller (the C of the MVC)

 public ActionResult Formulario1()
    {
        List<SelectListItem> anos = listaAnos();
        ViewBag.Anos = anos;// a passgem da lista é feita por ViewBag
        AnosModel modelo=new AnosModel();
        return View(modelo);
    }

This is a very simplified scenario, but it serves to illustrate the difference between the paradigms

Browser other questions tagged

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