Why isn’t my CONTROLLER picking up VIEW fields?

Asked

Viewed 541 times

2

I am making a form in the VIEW, but the fields where I use radio and select(option) are not passing their values to the Controller. If anyone knows why.

My view:

@model GerenciadorDeAtividades.Dominio.RecursoDominio

@{
    ViewBag.Title = "Cadastrar";
}

<div class="container">
    <div class="jumbotron">

        <h2>Cadastrar</h2>

        @using (Html.BeginForm())
        {
            @Html.AntiForgeryToken()
            @Html.ValidationSummary(true)

            <fieldset>
                <legend>Novo Recurso</legend>

                <div class="row">
                    <div class="col-md-9">
                        <div class="form-group">
                            @Html.LabelFor(model => model.Nome)
                            @Html.TextBoxFor(model => model.Nome, new { @class = "form-control", style = "width: 50%" })
                            @Html.ValidationMessageFor(model => model.Nome)
                        </div>

                        <div class="form-group">
                            @Html.LabelFor(model => model.Categoria)
                            <select class="form-control" style = "width: 40%">
                                <option value="Gerente">Gerente</option>
                                <option value="Coordenador">Coordenador</option>
                                <option value="Desenvolvedor">Desenvolvedor</option>
                                <option value="Funcional">Funcional</option>
                            </select>
                        </div>

                        <div class="form-group">
                            @Html.LabelFor(model => model.UsuarioSAP)
                            @Html.TextBoxFor(model => model.UsuarioSAP, new { @class = "form-control", style = "width: 40%"})
                            @Html.ValidationMessageFor(model => model.UsuarioSAP)
                        </div>

                        <div class="form-group">
                            @Html.LabelFor(model => model.UsuarioAD)
                            @Html.TextBoxFor(model => model.UsuarioAD, new { @class = "form-control", style = "width: 40%" })
                            @Html.ValidationMessageFor(model => model.UsuarioAD)
                        </div>

                        <div class="form-group">
                            @Html.LabelFor(model => model.Email)
                            <div class="input-group">
                                <span class="input-group-addon">&#64</span>
                                @Html.TextBoxFor(model => model.Email, new { @class = "form-control", style = "width: 37%" })
                                @Html.ValidationMessageFor(model => model.Email)
                            </div>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-group">
                            @Html.LabelFor(model => model.Desenvolvedor) <strong>?</strong>
                            <div class="radio">
                                <label>
                                    <input type="radio" name="radioDesenvolvedor" id="simD" value="True" checked>
                                    Sim
                                </label>
                            </div>
                            <div class="radio">
                                <label>
                                    <input type="radio" name="radioDesenvolvedor" id="naoD" value="False">
                                    Não
                                </label>
                            </div>
                        </div>

                        <div class="form-group">
                            @Html.LabelFor(model => model.Externo) <strong>?</strong>
                            <div class="radio">
                                <label>
                                    <input type="radio" name="radioExterno" id="simE" value="True" checked>
                                    Sim
                                </label>
                            </div>
                            <div class="radio">
                                <label>
                                    <input type="radio" name="radioExterno" id="naoE" value="False">
                                    Não
                                </label>
                            </div>
                        </div>
                    </div>
                </div>
                <p>
                    <button class="btn btn btn-primary" type="submit">Criar</button>
                    <button class="btn btn btn-primary" type="button" onclick="history.go(-1)">Cancelar</button>
                </p>
            </fieldset>
        }
    </div>
</div>

My Controller:

public ActionResult Cadastrar()
    {
        return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Cadastrar(RecursoDominio recurso)
    {
        if (ModelState.IsValid)
        {
            appRecurso.Salvar(recurso);
            return RedirectToAction("Index");
        }
        return View(recurso);
    }

1 answer

4

When you use tags without Html Helpers you must put the name of the property. in your case, you are just adding the ID. Thus, by giving the submit, your View does not recognize the property, thus not taking the value to its Controller.

However, how are you using Helpers, keep it up.

That would be your code:

     @model GerenciadorDeAtividades.Dominio.RecursoDominio

@{
    ViewBag.Title = "Cadastrar";
}

<div class="container">
    <div class="jumbotron">

        <h2>Cadastrar</h2>

        @using (Html.BeginForm())
        {
            @Html.AntiForgeryToken()
            @Html.ValidationSummary(true)

            <fieldset>
                <legend>Novo Recurso</legend>

                <div class="row">
                    <div class="col-md-9">
                        <div class="form-group">
                            @Html.LabelFor(model => model.Nome)
                            @Html.TextBoxFor(model => model.Nome, new { @class = "form-control", style = "width: 50%" })
                            @Html.ValidationMessageFor(model => model.Nome)
                        </div>

                        <div class="form-group">
                            @Html.LabelFor(model => model.Categoria)
                            @Html.DropDownListFor(model => model.Categoria, new List<SelectListItem>
                                    {
                                        new SelectListItem{Text = "Gerente", Value = "Gerente"},
                                        new SelectListItem{Text = "Coordenador", Value = "Coordenador"},
                                        new SelectListItem{Text = "Desenvolvedor", Value = "Desenvolvedor"},
                                        new SelectListItem{Text = "Funcional", Value = "Funcional"}
                                    }, new { @class = "form-control" })
                        </div>

                        <div class="form-group">
                            @Html.LabelFor(model => model.UsuarioSAP)
                            @Html.TextBoxFor(model => model.UsuarioSAP, new { @class = "form-control", style = "width: 40%"})
                            @Html.ValidationMessageFor(model => model.UsuarioSAP)
                        </div>

                        <div class="form-group">
                            @Html.LabelFor(model => model.UsuarioAD)
                            @Html.TextBoxFor(model => model.UsuarioAD, new { @class = "form-control", style = "width: 40%" })
                            @Html.ValidationMessageFor(model => model.UsuarioAD)
                        </div>

                        <div class="form-group">
                            @Html.LabelFor(model => model.Email)
                            <div class="input-group">
                                <span class="input-group-addon">&#64</span>
                                @Html.TextBoxFor(model => model.Email, new { @class = "form-control", style = "width: 37%" })
                                @Html.ValidationMessageFor(model => model.Email)
                            </div>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-group">
                            @Html.LabelFor(model => model.Desenvolvedor) <strong>?</strong>
                             @Html.RadioButtonFor( model => model.Desenvolvedor, true, new { id = "simD",  class="radio" } )
                            <label for="simD">Sim</label>
                            <br />
                            @Html.RadioButtonFor( model => model.Desenvolvedor, false, new { id = "naoD",  class="radio" } )
                            <label for="naoD">Não</label>
                        </div>

                    </div>
                </div>
                <p>
                    <button class="btn btn btn-primary" type="submit">Criar</button>
                    <button class="btn btn btn-primary" type="button" onclick="history.go(-1)">Cancelar</button>
                </p>
            </fieldset>
        }
    </div>
</div>

As you have not posted the entities, check if the property model => model.Categoria exists. And the same thing for the RadioButtonFor(). If you have different names, simply rename to the correct one.

Browser other questions tagged

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