Why is my CONTROLLER not picking VIEW fields?

2

I'm doing 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);
    }
    
asked by anonymous 14.05.2015 / 16:03

1 answer

3

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

However, since you're using Helpers , keep it up.

It would look like 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>

Since you have not posted the entities, check to see if the model => model.Categoria property exists. It's the same for RadioButtonFor() . If you have different names, just rename to the correct one.

    
14.05.2015 / 16:20