When entering edit mode, the edit should be filled in and only the city dropdownlist comes. See me 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();
}
}