View Field When Selecting Dropdownlist and Savar

0

I have the following scenario: In a form I have a field called Load-Time , which is a Dropdownlist with a list of time loads. In this drop an option called another. When the "Other" option is selected a new field is opened to enter this new workload.

I was able to do this normally with JavaScript in my form .

The question is, I would like that when it is saved in the database, instead of having a column called, "Another Load-Time", I would like to save this data in the existing Carga-Horaria column.

<div class="form-group">
    @Html.LabelFor(model => model.Nr_CHoraria, "Carga Horaria Semanal", new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownListFor(model => model.Nr_CHoraria, new List<SelectListItem>
             {
                 new SelectListItem() {Text = "12", Value="12"},
                 new SelectListItem() {Text = "20", Value="20"},
                 new SelectListItem() {Text = "24", Value="24"},
                 new SelectListItem() {Text = "30", Value="30"},
                 new SelectListItem() {Text = "36", Value="36"},
                 new SelectListItem() {Text = "40", Value="40"},
                 new SelectListItem() {Text = "44", Value="44"},
                 new SelectListItem() {Text = "Outra", Value="0"}
             }, new { @class = "form-control", @id = "ch" })
        @Html.ValidationMessageFor(model => model.Nr_CHoraria)
    </div>
</div>

<div class="form-group" style="display:none" id="div_dch">
    @Html.LabelFor(model => model.Nr_CHoraria, "Digite a Carga Horaria", new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.TextBoxFor(model => model.Nr_CHoraria, new { @class = "chs", @id = "dch" })
        @Html.ValidationMessageFor(model => model.Nr_CHoraria)
    </div>
</div>

<script type="text/javascript">
    $(function () {
        $('#ch').change(function () {
            var value = $(this).val();
            if (value == '0') {
                $('#div_dch').show();

            } else {
                $('#div_dch').hide();
            }
        });
    });

</script>
    
asked by anonymous 23.08.2016 / 16:37

1 answer

1

The correct thing is to separate the suggested load time field names and another time load. Something like this:

<div class="form-group">
    @Html.LabelFor(model => model.CargaHoraria, "Carga Horaria Semanal", new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownListFor(model => model.CargaHoraria, new List<SelectListItem>
             {
                 new SelectListItem() {Text = "12", Value="12"},
                 new SelectListItem() {Text = "20", Value="20"},
                 new SelectListItem() {Text = "24", Value="24"},
                 new SelectListItem() {Text = "30", Value="30"},
                 new SelectListItem() {Text = "36", Value="36"},
                 new SelectListItem() {Text = "40", Value="40"},
                 new SelectListItem() {Text = "44", Value="44"},
                 new SelectListItem() {Text = "Outra", Value="0"}
             }, new { @class = "form-control", @id = "ch" })
        @Html.ValidationMessageFor(model => model.CargaHoraria)
    </div>
</div>

<div class="form-group" style="display:none" id="div_dch">
    @Html.LabelFor(model => model.OutraCargaHoraria, "Digite a Carga Horaria", new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.TextBoxFor(model => model.OutraCargaHoraria, new { @class = "chs", @id = "dch" })
        @Html.ValidationMessageFor(model => model.OutraCargaHoraria)
    </div>
</div>

What I would do in its place is a ViewModel :

public class CargaHorariaViewModel
{
    public int CargaHoraria { get; set; }
    public int? OutraCargaHoraria { get; set; }
}

The Controller would receive this ViewModel :

[HttpPost]
public ActionResult CriarCargaHoraria(CargaHorariaViewModel viewModel)
{
    if (ModelState.IsValid)
    {
        var cargaHoraria = new CargaHoraria();
        if (viewModel.CargaHoraria > 0)
            cargaHoraria.Nr_CHoraria = viewModel.CargaHoraria;
        else {
            if (viewModel.OutraCargaHoraria == null) {
                ModelState.AddModelError("OutraCargaHoraria", "Se outra carga horária será informada, o valor de Outra Carga Informada não pode ser nulo. ");
                return View(viewModel);
            }

            cargaHoraria.Nr_CHoraria = (int)viewModel.OutraCargaHoraria;
        }

        // Coloque o resto da lógica aqui.
    }

    return View(viewModel);
}
    
23.09.2016 / 16:03