Dropdown field increment

Asked

Viewed 100 times

0

Well, I have the following problem.

I have to use a Javascript or Jquery function to add new fields to fill and save in the database. In these fields I have to use a Dropdown, but I don’t know how to get the data from the database. Utilizo Telerik.

What I got 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 idea?

  • 1

    Could you please post the code you are using?

  • Create new fields on the screen and submit this form to the server?

  • I posted just below the doubt

  • What’s the c#? code like you’re doing? You want to take the content of these drop down lists and add it to the database?

  • I am going through Viewbag the data.

2 answers

3

You haven’t added any information about your structure, so I’ll put a somewhat generic response.

This is another inevitable case for the use of Begincollectionitem.

First you need to install the Begincollectioniten via Nuget.

Install-Package Begincollectionitem

Done this, first creates a Action to get the new element from the list (by clicking +), this way:

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 the same. Your PartialView will be the same thing as the code you used on 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 name Disciplines of this part @using (Html.BeginCollectionItem("Disciplinas")) must be the same name as the virtual public virtual ICollection<Disciplina> Disciplinas{get;set;} in his Model.

In his View, we will change the form of "duplicate" the field, using a request ajax to the 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 to your code (performing the request on controller).

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

By doing this, by sending to save via POST normal, your Model will have a list of subjects.

  • I don’t like this approach, despite having already used it, it is a request to return a view without need. Nowadays there are better ways, such as using MVVM or even using jquery clone...

  • @Rod I think you’re mistaken. By using any of these methods you can easily see, but to send the data to the controller you need a communication. Only using the jQuery clone will not get the expected result.

  • I’m not making a mistake, you see, the reason why you don’t get to the controller a list, is because of the Binder model, Begin Collection, creates an index like Hidden, and the Names are being added guid, one way to also do is using clone yes! just do an index or sequentially do the Ames

  • @Rod That yes, but will in the same end, does not agree?

  • send the list yes, but with begincollection are request on the server, which could be avoided

  • Do not take on the other hand, your answer is correct, until I gave upvote, I just commented that there are other approaches, that would not need to make the request in partialview

  • @Rod Yes, I noticed and not for a second thought it was something "bad" his comments, on the contrary. But I do not see problems in the server request, until pq with this I can make validations if necessary, which could be a "fault" if done in the client side. But this does not come to the case of this question, just expanded a little... xD

Show 2 more comments

-2

Bruno,

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

Telerik has an API that helps you perform these client-side operations (browser).

Take a look, look for examples and don’t give up.

  • I’m looking, but I can’t find examples to save me. Rs

  • Bruno, in this case there are two operations. The first will be the inclusion of the record in the Database (Search for "How to insert a record via ajax .net"), The second step is, after the first has been successfully done as include the information of the new record in the Dropdown. Try the first one, then come back and ask how to do the second one :)

  • But I just want the data that is in the field to come in. I need more fields, it’s like an address. I have 1 address and I want to add the other, I have that "+" in front to add the other, only I don’t want to limit..

Browser other questions tagged

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