Text field does not take Asp.net MVC value

Asked

Viewed 266 times

0

I have a text field in my system called NomeEspecificacao, however when I fill any value in it, it is always returned null in my Action, ie never comes the word I write in the text field.

View code:

model SEMA.Fiscalizacao.Presentation.Web.Models.IncluirMaterialViewModel
@{
    var disabled = Model.Consultar ? "disabled=\"disabled\"" : "";
}

@{
    if (Model.Consultar)
    {
        <div class="row">
            <div class="col-md-12">
                <div class="form-group">
                    @Html.LabelFor(model => model.NomeMaterialCategoria, "Categoria")
                    @Html.EditorFor(model => model.NomeMaterialCategoria, new { htmlAttributes = new { @class = "form-control", @maxlength = 50, disabled } })
                </div>
            </div>
        </div>
    }
    else
    {
        <div class="row">
            <div class="col-md-12">
                <div class="form-group">
                    @Html.LabelFor(model => model.NomeMaterialCategoria, "Categoria")
                    @Html.EditorFor(model => model.NomeMaterialCategoria, new { htmlAttributes = new { @class = "form-control", @maxlength = 50 } })
                </div>
            </div>
        </div>
    }
}


@Html.Partial("_SelectMutipleUnidadesMedida", Model)

<hr class="hr-text" data-content="Especificação da Categoria" />

@{
    if (Model.Consultar)
    {
        <div class="row">
            <div class="col-md-12">
                @{
                    if (!string.IsNullOrWhiteSpace(Model.NomeMaterial) && Model.NomeMaterial.Length > 0)
                    {
                        @Html.TextBoxFor(model => model.NomeEspecificacao, new { @class = "form-control margin-bottom", @maxlength = 50 })
                    }
                    else
                    {
                        @Html.TextBoxFor(model => model.NomeEspecificacao, new { @class = "form-control margin-bottom", @maxlength = 50, disabled })
                    }
                }


            </div>
        </div>
    }
    else
    {
        <div class="row">
            <div class="col-md-12">
                @Html.TextBoxFor(model => model.NomeEspecificacao, new { @class = "form-control margin-bottom", @maxlength = 50 })
            </div>
        </div>

        <div class="row">
            <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 text-right margin-bottom">
                <input type="submit" id="AdicionarEspecificacao" value="Adicionar" class="btn btn-default" name="command:IncluirEspecificacoes:1">
                <button type="submit" id="AdicionaMultiplasEspecificacoes" class="btn btn-default" name="command:AbrirMultiplasEspecificacoes">
                    Adicionar Múltiplos
                </button>
            </div>
        </div>
    }
}


@if (Model.ListaMaterialCategoriaEspecificacoes != null && Model.ListaMaterialCategoriaEspecificacoes.Count > 0)
{
    <div class="row">
        <div class="col-md-12">
            @Html.Partial("_ListaEspecificacaoCategoria", Model)
        </div>
    </div>
}

Action that returns null in the model:

[HttpPost]
        [CommandName("Alterar", "command:IncluirEspecificacoes:{index}")]
        public async Task<ActionResult> IncluirEspecificacoesAlterarAsync(IncluirMaterialViewModel model)
        {
            var tipoEspc = Convert.ToInt32(RouteData.Values["index"]);


            if (tipoEspc == 1)
            {

                    AdicionaEspecificacao(model, model.NomeEspecificacao);
            }

            if (tipoEspc == 2)
            {
                if (string.IsNullOrWhiteSpace(model.NomeEspecificacaoMultiplo))
                {
                    model.ModalEspecificacoes = true;
                    ErrorMessage = Mensagens.UC027_MSG007;

                    model.CodigoUnidadeMedidaPermitidaUsoSimpViewModel
               .AddRange(model.ListaUnidadeMedidasPermitidasParaUso.Select(x => x.CodigoUnidadeMedida));

                    await PreencherUnidadesMedidaViewModelIncluir(model);
                    await AdicionaUnidadeMedidaPermitidasParaUso(model);

                    return PartialView("_Incluir", model);
                }
                else
                {
                    AdicionaEspecificacao(model, model.NomeEspecificacaoMultiplo);
                }
            }


            model.CodigoUnidadeMedidaPermitidaUsoSimpViewModel
                .AddRange(model.ListaUnidadeMedidasPermitidasParaUso.Select(x => x.CodigoUnidadeMedida));

            await PreencherUnidadesMedidaViewModelIncluir(model);
            await AdicionaUnidadeMedidaPermitidasParaUso(model);
            return PartialView("_Incluir", model);
        }

Imagem do campo preenchido

Resultado vindo na Model logo após clicar em "Adicionar"

Someone can help me?

Ps: added images for a better analysis of the problem.

  • What does the request look like when you submit the form? This field is present in it?

  • yes is present

  • I added images to show you what’s going on....

  • is for editing only. For inclusion it works...

1 answer

1

Do not disable control, set it to readonly and apply CSS styles to look disabled.

Disabled fields are not posted. That’s why Defaultmodelbinder cannot fill the corresponding property of your model.

  • I commented the var disabled line = Model.Query ? "disabled="disabled"": ""; and removed all "disabled" from the fields. Continues as null in the model. Does not take values filled in field.

  • I added images to show you what’s going on....

  • I don’t know if it has anything to do with....

Browser other questions tagged

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