COMO CRIAR DROPDOWNLIST USANDO C# MVC.NET

Fala galera blz?

Faz tempo que não posto nada de programação, o motivo é porque estou estudando mais inglês esses últimos dias hehehe

Então, primeiramente ja peço desculpas ok? Mas prometo que vou postar mais, pois reestruturei o meu canal no youtube para tutoriais de programação C# mvc.net blz?

Segue o link do Projeto C# em MVC.NET pronto para download: http://adf.ly/1eNn21

Segue o link do video no meu canal do youtube onde eu explico detalhadamente como criar e manipular o DROPDOWNLIST:



Se não quiser ver o vídeo e fazer na raça, estão aqui os códigos principais que você precisará:

Para criar o dropdownlist você precisará:

1) Utilizar a seguinte sintaxe na sua view:
@Html.DropDownListFor(prop => prop.SelectedTeacher, new SelectList(this.Model.lstTeachers, "ID", "Name"), "Select the teacher", new { @id = "myFirstDropdownlist", @onchange= "nomeQualquer();" })

2) Para o evento onchange chamar uma função javascript que faça o postback, você poderá criar da seuginte forma:
<script type="text/javascript">
    function nomeQualquer() {
        document.getElementById("formIdTest").submit();
    }
</script>

3) O método dentro do controller que carrega a lista de professores ficou assim:
 private void LoadTeachers(TestModel model)
        {
            var lstTeachers = new List<Teacher>()
            {
                new Teacher()
                {
                    ID = 1, Name ="Caio", Surname = "Uechi"
                },
                   new Teacher() {ID = 2, Name ="Nana", Surname = "Bravo" },
                   new Teacher() {ID = 3, Name ="Irir", Surname = "Honta" },
                   new Teacher() {ID = 4, Name ="Verver", Surname = "SVer" },
                   new Teacher() {ID = 5, Name ="George", Surname = "Brother" }
            };

            model.lstTeachers = lstTeachers;
        }

4) O método que criou a lista de matérias assim:
private void LoadSubjects(TestModel testModel)
        {
            var lstSubjects = new List<Subject>()
            {
                new Subject()
                {
                    ID = 1, Name = "SUBJECTcaio", teacherResponsibleForThisSubjectID = 1
                },
                   new Subject() {ID = 2, Name ="SUBJECTaaa", teacherResponsibleForThisSubjectID = 1 },
                   new Subject() {ID = 3, Name ="SUBJECTbbb", teacherResponsibleForThisSubjectID = 1 },
                   new Subject() {ID = 4, Name ="SUBJECTccc", teacherResponsibleForThisSubjectID = 2 },
                   new Subject() {ID = 5, Name ="SUBJECTddd", teacherResponsibleForThisSubjectID = 3 },
                   new Subject() {ID = 6, Name ="SUBJECTeee", teacherResponsibleForThisSubjectID = 4 },
                   new Subject() {ID = 7, Name ="SUBJECTgeorge", teacherResponsibleForThisSubjectID = 5 }
            };

            testModel.lstSubjects = lstSubjects.Where(prop=>prop.teacherResponsibleForThisSubjectID == testModel.SelectedTeacher).ToList();

        }

5) Não se esqueça de alterar o controle test com a requisição Get e Post:
        public ActionResult Test()
        {
            TestModel testModel = new TestModel();

            LoadTeachers(testModel);

            return View(testModel);
        }

        [HttpPost]
        public ActionResult Test(TestModel testModel)
        {
            LoadTeachers(testModel);
            LoadSubjects(testModel);

            return View(testModel);
        }

        public ActionResult Index()
        {
            return View();
        }

Espero que tenham gostado galera! Se puder, por favor de um like ou compartilhe para dar aquela força e eu continuar postando blz?? valeu!!

Comentários

Postagens mais visitadas deste blog

Lista Enkas Músicas japonesas que você deve escutar antes de morrer

SOLVED MySQL connector with ENTITY FRAMEWORK WORKING in Medium Trust hosting environments

SUS nota 10 - Exames gratuitos DST para saber se tem HIV-AIDS SIFILIS e HEPATITE C em 1 hora