脚手架控制器没有为MVC 5

时间:2015-12-07 19:39:54

标签: c# asp.net-mvc asp.net-mvc-scaffolding

我使用Scaffolding在我的MVC 5项目中创建第一个版本的控制器,但是我无法在两个实体之间创建关系并获得预期的结果。

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; }
}

和通话类型

[Table("CallTypes")]
public partial class CallType
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Display(Name = "Nome")]
    public string Name { get; set; }
}

但是,呼叫创建和编辑视图没有创建带有呼叫类型的下拉列表,为什么?

编辑:

对于UserCreated,我正确地得到了下拉列表:

查看:

<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>

控制器:

public virtual ActionResult Create()
{
    ViewBag.UserCreated_Id = new SelectList(db.ApplicationUsers, "Id", "Email");
    return View();
}

但是对于CallType我没有得到任何代码。

EDIT2:

我找到了问题的解决方案,我想知道是不是这样:

[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
}

现在我正确地获得了代码:

// 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();
}

观点:

<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>

0 个答案:

没有答案