使用CodeFirst与EF努力让下拉列表工作

时间:2011-10-20 13:38:44

标签: asp.net-mvc entity-framework entity-framework-4.1

我正在尝试开发一个相当简单的应用程序,用于办公室和我自己的培训目的。

目前我有点担心会出现一个下拉列表。

数据结构

表:资源
ResourceID(int)(PK)
名称(varchar)
描述(varchar)
ResourceTypeID(int)(FK)

表格: REsourceType
ResourceTypeID(int)(PK)
标题(varchar)
描述(varchar)

这是一个简单的数据结构。每个资源都是一种类型,但每种类型都可以应用于许多资源。我有3个模型文件:

资源

namespace ESF_ResourceManager.Models
{
    public class Resource
    {
        [Key]
        public int ResourceID { get; set; } 

        [Required(ErrorMessage="Please enter a name for the resource")]
        [StringLength(50, ErrorMessage="Resource name is too long, 50 characters or less")]
        public string Name { get; set; }

        [Required(ErrorMessage="Please neter a meaningful description of this resource")]
        public string Description { get; set; }

        [Required(ErrorMessage="Please specify the turn around time in minutes")]
        public int TurnAroundTime { get; set; }

        // links resource to a resource type
        public int ResourceTypeID { get; set; }


         //public virtual ICollection<ResourceType> ResourceTypes { get; set; }
        // public virtual ResourceType ResourceTypeID { get; set; }
    }
}

的ResourceType

namespace ESF_ResourceManager.Models
{
    public class ResourceType
    {
        [Key]
        public int ResourceTypeID { get; set; }

        [Required(ErrorMessage = "Please enter a title for the resource type")]
        [StringLength(50, ErrorMessage = "Resource type name is too long, 50 characters or less")]
        public string Title { get; set; }

        [Required(ErrorMessage = "Please enter a meaningful description for the resource type")]
        public string Description { get; set; }

        public virtual ICollection<Resource> Resources { get; set; }
        //public virtual Resource Resource {get; set;}
    }
}

ResourceAdminManager

namespace ESF_ResourceManager.Models
{
    public class ResourceAdminManager : DbContext
    {
        public DbSet<Resource> Resources { get; set; }
        public DbSet<ResourceType> ResourceTypes { get; set; }
    }
}

我为Resource和ResourceType开发了视图和控制器。 ResourceType非常直接,而且该套装工作正常。我遇到的麻烦是了解如何获取Create和Edit的Resource视图以在下拉列表中显示ResourceType的选项,以及如何在所有视图中显示ResourceType的标题而不是ID。

我花了很长时间来看这个并且还没有发现任何我理解的辅助工具。所以,请新手一起轻松一下,我相信我会帮助你。

非常感谢 nathj07

2 个答案:

答案 0 :(得分:2)

查看模型 - CreateResourceViewModel.cs:

public class CreateResourceViewModel
{
    public Resource Resource { get; set; }
    public ICollection<ResourceType> ResourceTypes { get; set; }
} 

Controller(ResourceController.cs):

[HttpGet]
public ActionResult Create()
{
    var dbContext = new ResourceDbContext();

    var model = new CreateResourceViewModel
                    {
                        Resource = new Resource(),
                        ResourceTypes = dbContext.ResourceTypes.ToList()
                    };

    return View(model);
}

[HttpPost]
public ActionResult Create(CreateResourceViewModel model)
{
    // process model

    return RedirectToAction("Success");
}

查看(Create.cshtml):

@model ResourceMVC.Models.CreateResourceViewModel

<div class="editor-label">
    @Html.LabelFor(x => x.Resource.ResourceTypeId)
</div>

<div class="editor-field">
    @Html.DropDownListFor(x => x.Resource.ResourceTypeId, 
                          new SelectList(Model.ResourceTypes, "ResourceTypeId", "Title"),
                          "-- Select Resource Type --")
</div>

这会将所选Id的{​​{1}}绑定到新ResourceType对象的ResourceTypeId,并将其发回Resource。< / p>

答案 1 :(得分:0)

您还没有真正将ResourceTypes与资源相关联。

添加到资源:

[Include]
[Association("GiveItAName", "ResourceID", "ResourceTypeID")]
public EntityCollection<ResourceType> ResourceTypes { get; set; }

[Include]
[Association("GiveItAName", "ResourceID", "ResourceTypeID")]
public ResourceType ResourceType { get; set; }

使用[ForeignKey]属性将ID字段标记为FK字段。

然后您应该能够执行MyResource.ResourceType.Title之类的操作使用Fiddler和WCF二进制加载项来验证EF是否正在返回对象并正确包含子元素。