I use Scaffolding to create the first version of controllers in my MVC 5 projects, but I'm not able to create the relashionship between 2 entities and get the expected results.
public partial class Call
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Display(Name = "Criado em")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-M-yyyy HH:mm}")]
public DateTime DateCreated { get; set; }
[Display(Name = "Alterado em")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-M-yyyy HH:mm}")]
public DateTime DateModified { get; set; }
public int UserCreated_Id { get; set; }
[ForeignKey("UserCreated_Id")]
public ApplicationUser UserCreated { get; set; }
public int UserModified_Id { get; set; }
[ForeignKey("UserModified_Id")]
public ApplicationUser UserModified { get; set; }
[Required(ErrorMessage = "Campo obrigatório")]
[MaxLength(256, ErrorMessage = "Não pode ter mais que 256 caracteres")]
[Display(Name = "Assunto")]
public string Subject { get; set; }
[Display(Name = "Descrição")]
public string Description { get; set; }
[Display(Name = "Data e Hora de início")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-M-yyyy HH:mm}")]
public DateTime DateToCall { get; set; }
public int CallType_Id { get; set; }
[ForeignKey("CallType_Id")]
public CallType CallType { get; set; }
}
And Call Type
[Table("CallTypes")]
public partial class CallType
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Display(Name = "Nome")]
public string Name { get; set; }
}
But the Call Create and Edit Views are not creating the Dropdownlist with the Call Types, why?
EDIT:
For the UserCreated I get the dropdownlist correctly:
View:
<div class="form-group">
@Html.LabelFor(model => model.UserCreated_Id, "UserCreated_Id", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("UserCreated_Id", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.UserCreated_Id, "", new { @class = "text-danger" })
</div>
</div>
Controller:
public virtual ActionResult Create()
{
ViewBag.UserCreated_Id = new SelectList(db.ApplicationUsers, "Id", "Email");
return View();
}
But for the CallType I don't get any code.
EDIT2:
I found a solution for the problem, I would like to know if it's the way:
[Table("CallTypes")]
public partial class CallType
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Display(Name = "Nome")]
public string Name { get; set; }
public virtual ICollection<Call> Calls { get; set; } // <--- Add the relation with the Call Entity
}
Now I get the code correctly:
// GET: Call/Create
public virtual ActionResult Create()
{
ViewBag.CallType_Id = new SelectList(db.CallTypes, "Id", "Name");
ViewBag.UserCreated_Id = new SelectList(db.ApplicationUsers, "Id", "Email");
ViewBag.UserModified_Id = new SelectList(db.ApplicationUsers, "Id", "Email");
return View();
}
And the View:
<div class="form-group">
@Html.LabelFor(model => model.CallType_Id, "CallType_Id", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("CallType_Id", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CallType_Id, "", new { @class = "text-danger" })
</div>
</div>