Edit data from a combo box

Asked

Viewed 895 times

3

I’m having a problem with my Asp.net mvc application. What’s going on is that when it comes to saving the data, my combo boxes carry data from enums. These combo boxes are from an application to a school, where the series/year, class and shift are loaded. But the problem I’m having is that, at the time I’m going to edit this data that was saved, these combo boxes return to the default value, that is, the data that is already loaded. My question is: how do I make so that when trying to change this data, they load the data that has already been saved, because all other data load normally, only the combos boxes that have this, and detail, if by chance I do not tidy the data the way it was before editing, the combo data is changed to the default values.

I’ll post their code here:

Year/Series

 <div class="form-group">
                @Html.LabelFor(model => model.Ano, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    <select name="Ano">
                        @for (int i = 5; i < 10; i++)
                        {
                            <option>@i</option>
                            <p>º</p>
                        }
                    </select>
                    @Html.ValidationMessageFor(model => model.Ano)
                </div>
            </div>

Gang

 <div class="form-group">
                @Html.LabelFor(model => model.Turma, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    <select name="Turma">
                        @foreach (var item in ViewBag.Turmas)
                        {
                            <option>@item</option>
                        }
                    </select>
                    @Html.ValidationMessageFor(model => model.Turma)
                </div>
            </div>

Shift

 <div class="form-group">
                @Html.LabelFor(model => model.Turno, new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    <select name="Turno">
                        @foreach (var item in ViewBag.Turno)
                        {
                            <option>@item</option>
                        }
                    </select>
                    @Html.ValidationMessageFor(model => model.Turno)
                </div>
            </div>

1 answer

1


You can upload this information in other ways by letting the framework work for you. Use Selectlist for such functionality.

Explanation inserir a descrição da imagem aqui

In that Selectlist we have to inform:

  • items: a list of values

  • dataValueField: a value that identifies select internally the same as <option value="dataValueField"> being the same value.

  • dataTextField: a value that symbolises the description between the <option>dataTextField</option>.

  • selectedValue: information used when you want to position the select in a given item, usually used in registry changes and is the tag inside the option selected="selected" thus: <option selected="selected"></otpion>

Example:

No Controller

public ActionResult Index()
{
    Dictionary<int, int> Anos = new Dictionary<int, int>();
    for (int i = 5; i < 10; i++){
        Anos.Add(i, i);
    }
    ViewBag.DropAno = new SelectList(Anos.AsEnumerable(), "key", "value", 5);
    return View();
}

In the View of this Controller

@Html.DropDownList("DropAno")

Automatically it will load this select for you as shown in the figure below

inserir a descrição da imagem aqui

Generated html code:

<select id="DropAno" name="DropAno">
   <option value="5">5</option>
   <option value="6">6</option>
   <option value="7">7</option>
   <option value="8">8</option>
   <option value="9">9</option>
</select>

Another example with a list of some class

Within the Controller Method

List<Pessoa> pessoas = new List<Pessoa>();
pessoas.Add(new Pessoa(1, "Nome 1"));
pessoas.Add(new Pessoa(2, "Nome 2"));
pessoas.Add(new Pessoa(3, "Nome 3"));
ViewBag.DropPessoas = new SelectList(pessoas.AsEnumerable(), "Id", "Nome");

View from that controller method

@Html.DropDownList("DropPessoas")

Load this Selectlist positioned on the change item

List<Pessoa> pessoas = new List<Pessoa>();
pessoas.Add(new Pessoa(1, "Nome 1"));
pessoas.Add(new Pessoa(2, "Nome 2"));
pessoas.Add(new Pessoa(3, "Nome 3"));
int Numero = 3; // automaticamente ele vai carregar e posicionar o select no numero 3
ViewBag.DropPessoas = new SelectList(pessoas.AsEnumerable(), "Id", "Nome", Numero);

In this last example of code you realize that another item has been passed Numero this being the item that came as default from your database to change, ie, it will load the select and position.

References

  • In the controller that was mentioned there, would it be Edit and create ? Or can you put it in the same index ? Or just Edit, where I’m having problems ?

  • @Erikthiago put in the controller method that will load your view. Example: if your view is Edit put in the Edit method

  • Exactly, I’m only having trouble with it. It doesn’t load the data that was registered. It always loads the default data. But if I put this method there on Dit, I’ll need to put it this way in create as well ? No right ?

  • You will need to put every time you use, if in your create you have this item then you have to use it too!

  • Aah, got it ! So I have to change my Edit controller and create. Just another doubt, this part here: "Dictionary<int, int> Years = new Dictionary<int, int>();", Dictionary would be my right Enum ?

  • @Eric may be I don’t know how your application is because you just put your view, but the logic is this can use

  • You want me to put the parts of the controller that have Edit and create it to make it easier ? If you want to put the enums that use it too ! The year is the only one I use without Enum !

Show 3 more comments

Browser other questions tagged

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