Edit screen does not load data on screen

Asked

Viewed 48 times

-1

When entering edit mode, the Dits should be filled in and only the city dropdownlist comes. See cshtml below.

@model TreinamentoCrud.FuncViewModel.FuncionarioViewModel

@{
    ViewBag.Title = "EditVM";
}

<h2>EditVM</h2>


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

    <div class="form-horizontal">
        <h4>FuncionarioViewModel</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.id)

        <div class="form-group">
            @Html.LabelFor(model => model.nome, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.nome, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.nome, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.dataNascimento, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.dataNascimento, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.dataNascimento, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.cpf, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.cpf, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.cpf, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.cidade, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("cidade", (SelectList)ViewBag.ViewCidade, "- Selecione a cidade -", new { @class = "form-control" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Voltar", "IndexVM")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

and this is the controller

public async Task<ActionResult> EditVM(int id)
        {
            GetCidadesAsync cidade = new GetCidadesAsync();
            var _cidade = await cidade.GetCidades();

            ViewBag.ViewCidade = new SelectList
            (
                _cidade,
                "id",
                "nome" ,
                id
            );

            return View();
        }

        // POST: FuncViewModel/Edit/5
        [HttpPost]
        public async Task<ActionResult> EditVM(int id, FuncionarioViewModel funcionario)
        {
            PutFuncionarioAsync putFuncionario = new PutFuncionarioAsync();

            try
            {
                await putFuncionario.PutFuncionarioVM(funcionario);

                return RedirectToAction("IndexVM");
            }
            catch
            {
                return View();
            }
        }

1 answer

0

Missed to return the object to the View, your code would be more or less like this:

public async Task<ActionResult> EditVM(int id)
{
    GetFuncionarioAsync funcionario = new GetFuncionarioAsync();
    var _funcionario = await funcionario.GetFuncionario(id);


    GetCidadesAsync cidade = new GetCidadesAsync();
    var _cidade = await cidade.GetCidades();

    ViewBag.ViewCidade = new SelectList
    (
        _cidade,
        "id",
        "nome" ,
        id
    );

    return View(_funcionario);
}

I don’t know if the names of the objects are correct, it was a hunch, maybe you have to adjust

Browser other questions tagged

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