DropDown field increment

0

Well, I have the following problem.

I have to use a function in JavaScript or JQuery to add new fields to populate and save in the database. In these fields I have to use a DropDown, but I do not know how I'll get the data from the database. I use telerik.

What I have is this

$("#incremento").click(function () {

var acrescentar = '@Html.DropDownListFor(model => model.Id_dis, new SelectList(ViewBag.Disciplina, "Id_dis", "Nome"), new { @class = "form-control" })';

$('#etapa').append(acrescentar);

});


var disciplina = from disci in Documento_Caract_BD.DocumentoCaractConnection.Model.
                        Tb_disciplinas.OrderBy(o => o.Nome).ToList()
                        select new
                        {
                            Id_dis = disci.Id_dis,
                            Nome = disci.Nome
                        }
                            into myDisciplina
                            select myDisciplina;

            ViewBag.Disciplina = disciplina.ToList();

Does anyone have any ideas?

    
asked by anonymous 16.12.2015 / 18:26

2 answers

3
  

You have not added information about your structure, so I'll put a somewhat generic answer.

This is yet another unavoidable case for using the BeginCollectionItem .

First you need to install BeginCollectionIten via NuGet .

  

Install-Package BeginCollectionItem

Once this is done, first create a Action to get the new element from the list (by clicking on the +), like this:

public ActionResult ObterDisciplina()
    {
       ViewBag.Disciplina = disciplina.ToList();//Adiciona o método para povoar do DropDownList aqui
        var disciplina = new Disciplina();

        return PartialView("_Disciplina", disciplina);
    }

In this Action we are creating a new discipline and returning a PartialView with it. Your PartialView will be the same as the code you used in append , in your question. It will look like this:

@model Projeto.Disciplina//Altere isso para seu Model

@using (Html.BeginCollectionItem("Disciplinas"))//O nome da sua lista aqui
{
   @Html.DropDownListFor(/*A pripriedade que irá ser salva aqui e a ViewBag*/);
}

The Disciplines name of this part @using (Html.BeginCollectionItem("Disciplinas")) must be the same name as the virtual public virtual ICollection<Disciplina> Disciplinas{get;set;} in your Model.

In your View , we will change how to "duplicate" the field, using a ajax request for controller .

<script>
    $('#incremento').click(function () {
        $.ajax({
            url: '@Url.Action("ObterDisciplina", "NOMECONTROLLER")',
            type: 'POST',
            success: function (data) {
                $('#etapa').append(data);
            }
        });
    });

</script>

In this request we are adding a discipline in your code (making the request in controller ).

Note that in this part $('#etapa').append(data); we are doing append() of PartialView() for element with id="etapa" .

By doing this, when you save via POST normal, your Model will have a list of disciplines.

    
16.12.2015 / 19:43
-2

Bruno,

You are confused things. The HTML helper will run on the server, you will work with the result of running the browser.

Telerik has an API that helps you do these operations on the client side (browser).

Take a look, look for examples and do not give up.

    
16.12.2015 / 18:59