将 CodeFirst 与 EF 结合使用,努力使下拉列表正常工作
我正在尝试开发一个相当简单的应用程序,供在办公室使用和我自己的培训目的。
目前,我对显示下拉列表有点困惑。
数据结构
表:资源
资源ID(整数)(PK)
名称(varchar)
描述(varchar)
ResourceTypeID (int) (FK)
表:REsourceType
资源类型ID (int) (PK)
标题(varchar)
描述 (varchar)
一个简单的数据结构。每个资源都是一种类型,但每种类型可以应用于多种资源。我有 3 个模型文件:
Resource
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 非常简单,并且该集工作得很好。我遇到的麻烦是了解如何获取“创建”和“编辑”的资源视图以在下拉列表中显示 ResourceType 的选项,以及如何在所有视图中显示 ResourceType 的标题而不是 ID。
我花了相当长的时间研究这个问题,但还没有找到任何有助于我理解的东西。所以,作为一个新手,请慢慢来,我相信在你的帮助下我会成功的。
非常感谢 纳斯吉07
I'm trying to develop a fairly simple app, for use in the office and for my own training purposes.
At present I am a little stuck with getting a dropdownlist to appear.
The Data Structure
table: Resource
ResourceID (int) (PK)
Name (varchar)
Description (varchar)
ResourceTypeID (int) (FK)
table: REsourceType
ResourceTypeID (int) (PK)
Title (varchar)
Description (varchar)
So a simple datastructure. Each Resource is of one type, but each type can be applied to many resources. I have 3 model files:
Resource
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; }
}
}
I have developed the views and controllers for both the Resource and ResourceType. ResourceType is really straight forward and that set is working just fine. The trouble I am having is understanding how to get the Resource views for Create and Edit to display the options for ResourceType in a drop down list and how to display the title of the ResourceType not the ID in all the views.
I have spent quite some time looking over this and have not yet found anything the aids my understanding. So, please as a newbie take it easy with me and I'm sure I'll get there with your help.
Many thanks
nathj07
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
视图模型 - CreateResourceViewModel.cs:
控制器 (ResourceController.cs):
视图 (Create.cshtml):
这会将所选
ResourceType
的Id
绑定到ResourceTypeId
新的Resource
对象被 POST 回ResourceController
时。View Model - CreateResourceViewModel.cs:
Controller (ResourceController.cs):
View (Create.cshtml):
This will bind the
Id
of the selectedResourceType
to theResourceTypeId
of the newResource
object when it is POSTed back to theResourceController
.您还没有真正将 ResourceTypes 与资源关联起来。
添加到资源:
或
使用[ForeignKey]属性将ID字段标记为FK字段。
然后,您应该能够执行
MyResource.ResourceType.Title
等操作。使用 Fiddler 和 WCF 二进制加载项来验证 EF 是否返回对象并正确包含子元素。You haven't really associated the ResourceTypes to a Resource yet.
Add to Resource:
or
Use [ForeignKey] attribute to mark the ID fields as FK field.
Then you should be able to do things like
MyResource.ResourceType.Title
Use Fiddler and the WCF Binary add-in to verify that EF is returning the objects and properly including the child elements.