Edit data for a combo box

3

I'm having a problem with my application in ASP.NET MVC. What's happening is that when it comes to saving data, my combo boxes load data from enums. These combo boxes are from an application to a school, where the series / year, the class and the shift are loaded in them. But the problem I'm having is that, at the time I'm going to edit those data that have been saved, these combo boxes return to the default value, that is, the data that is already loaded. My question is: how do I do that when trying to change this data, they load the data that has already been saved, since all other data loads normally, only the combos boxes that have this, and detail, if by chance I do not to sort the data the way it was before editing, the combos data is changed to 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>

Class

 <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>
    
asked by anonymous 01.05.2014 / 19:51

1 answer

1

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

Explanation

Inthis SelectList we have what to report:

  • items : a list of values

  • DataTextField : A value that symbolizes the description that lies between <option value="dataValueField"> .

  • selectedValue : information used when you want to position the select in a particular item, usually used in registry changes, and stays as a tag within the option <option>dataTextField</option> so : selected="selected"

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 to you as shown in the figure below

HTMLcodegenerated:

<selectid="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

Inside 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 of that controller method

@Html.DropDownList("DropPessoas")

Load this SelectList positioned in 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 code example, you will notice that a second item has been passed <option selected="selected"></otpion> and this is the item that came as the default of your database to change, that is, it will load the select and position.

References

01.05.2014 / 21:54