将 CodeFirst 与 EF 结合使用,努力使下拉列表正常工作

发布于 2024-12-10 20:41:40 字数 2558 浏览 0 评论 0原文

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

目前,我对显示下拉列表有点困惑。

数据结构

表:资源
资源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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一个人练习一个人 2024-12-17 20:41:40

视图模型 - CreateResourceViewModel.cs:

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

控制器 (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>

这会将所选 ResourceTypeId 绑定到 ResourceTypeId 新的 Resource 对象被 POST 回 ResourceController 时。

View Model - 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");
}

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

This will bind the Id of the selected ResourceType to the ResourceTypeId of the new Resource object when it is POSTed back to the ResourceController.

橘香 2024-12-17 20:41:40

您还没有真正将 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 是否返回对象并正确包含子元素。

You haven't really associated the ResourceTypes to a Resource yet.

Add to Resource:

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

or

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文