The correct way is by using ViewModel :
namespace SeuProjeto.ViewModels
{
public class CriarEvolucaoViewModel
{
public Usuario Usuario { get; set; }
public Chamado Chamado { get; set; }
public Evolucao Evolucao { get; set; }
}
}
In Action GET
, you will have to bring all possible records and initialize the ViewModel object:
public async Task<ActionResult> Create()
{
ViewBag.UsuariosPossiveis = db.Usuarios.ToList();
ViewBag.ChamadosPossiveis = db.Chamados.ToList();
return View(new CriarEvolucaoViewModel
{
Usuario = new Usuario(),
Chamado = new Chamado(),
Evolucao = new Evolucao()
});
}
View :
@model SeuProjeto.ViewModels.CriarEvolucaoViewModel
@using SeuProjeto.Models
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Evolucao</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Usuario, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.Usuario.UsuarioId,
((IEnumerable<Usuario>)ViewBag.UsuariosPossiveis).Select(option => new SelectListItem
{
Text = option.Nome,
Value = option.UsuarioId.ToString()
}),
new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Usuario.UsuarioId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Chamado, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.Chamado.ChamadoId,
((IEnumerable<Usuario>)ViewBag.ChamadosPossiveis).Select(option => new SelectListItem
{
Text = option.Titulo,
Value = option.ChamadoId.ToString()
}),
new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Chamado.ChamadoId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Texto, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextAreaFor(model => model.Texto, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Texto, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
The Action of Controller that will get this is something like this:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create(CriarEvolucaoViewModel viewModel)
{
if (ModelState.IsValid)
{
// Aqui você pode fazer verificações adicionais, se quiser.
var novaEvolucao = new Evolucao
{
EvolucaoId = Guid.NewGuid(),
Usuario = db.Usuarios.Single(u => u.UsuarioId == viewModel.Usuario.UsuarioId),
Chamado = db.Chamados.Single(c => c.ChamadoId == viewModel.Chamado.ChamadoId),
}
db.Evolucoes.Add(novaEvolucao);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(viewModel);
}
The advantage of this method is that you can also create Usuario
and Chamado
on the same screen, only by placing the fields appropriately and modifying the insertion logic.
If you are not going to enter Usuario
and Chamado
, you can do it simpler:
public async Task<ActionResult> Create()
{
ViewBag.UsuariosPossiveis = db.Usuarios.ToList();
ViewBag.ChamadosPossiveis = db.Chamados.ToList();
return View();
}
View :
@model SeuProjeto.Models.Evolucao
@using SeuProjeto.Models
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Evolucao</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Usuario, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.UsuarioId,
((IEnumerable<Usuario>)ViewBag.UsuariosPossiveis).Select(option => new SelectListItem
{
Text = option.Nome,
Value = option.UsuarioId.ToString()
}),
new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.UsuarioId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Chamado, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.ChamadoId,
((IEnumerable<Usuario>)ViewBag.ChamadosPossiveis).Select(option => new SelectListItem
{
Text = option.Titulo,
Value = option.ChamadoId.ToString()
}),
new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ChamadoId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Texto, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextAreaFor(model => model.Texto, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Texto, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Action POST
:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create([Bind(Include = "ChamadoId,UsuarioId,Texto")]Evolucao evolucao)
{
if (ModelState.IsValid)
{
// Aqui você pode fazer verificações adicionais, se quiser.
db.Evolucoes.Add(evolucao);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
return View(evolucao);
}