1

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>
Patrick
  • 2,995
  • 14
  • 64
  • 125
  • What is it creating for it; nothing? – ChiefTwoPencils Dec 07 '15 at 19:45
  • Hi, Thanks! Yes it's correct, nothing. But for the UserCreated it works fine. – Patrick Dec 07 '15 at 19:46
  • Can you please describe the issue in more detail? Scaffolding doesn't typically create any drop down lists for related entities, as that would require additional query logic not related to the entity being scaffolded. – Chris Pratt Dec 07 '15 at 20:03
  • @ChrisPratt Hi Thanks! I have edited the question with the code generated for the UserCreated and regarding the CallState I don't get anything – Patrick Dec 07 '15 at 20:07
  • Are you saying that the scaffold actually generated that `ViewBag` property for you? I confess I haven't used scaffolding in a while, but back when I did, it never did that, and I doubt it does now. – Chris Pratt Dec 07 '15 at 20:13
  • @ChrisPratt I have copy the code directly from a generated View and Controller using MVC 5 Scaffold – Patrick Dec 07 '15 at 20:14
  • Try Adding ".ToList()" => ViewBag.UserCreated_Id = new SelectList(db.ApplicationUsers.ToList(), "Id", "Email"); – Golois Mouelet Dec 07 '15 at 20:17
  • @GoloisMouelet Hi, thanks but I don't understand the context of your sugestion, what do you mean? – Patrick Dec 07 '15 at 20:18
  • @Patrick, when setting the ViewBag, you are passing the list of application user as IQueryable, so instead convert that to a list: db.ApplicationUsers.ToList() as you set the viewbag. Just a suggestion – Golois Mouelet Dec 07 '15 at 20:23
  • @GoloisMouelet Ok thank you for the suggestion. – Patrick Dec 07 '15 at 20:24
  • @ChrisPratt Please check my EDIT2 and tell me what do you think about it. Thanks. – Patrick Dec 07 '15 at 20:32
  • 1
    I guess scaffolding does generate a dropdown for you as long as there's a collection on the other end, it seems. If it works for you, then go for it. However, it's best not to be so reliant on scaffolding anyways, and in particular, you should avoid using `ViewBag` for these sorts of things. The scaffold does it because it's utilizing the entity directly rather than a view model, so it has no other option, but it's not a best practice, and you should actually be using view models rather than entity classes directly. – Chris Pratt Dec 07 '15 at 20:37
  • @ChrisPratt Ok, thank you for your help. – Patrick Dec 07 '15 at 20:38

0 Answers0