Mvc3 列表框用于在编辑实体时辅助方法选择项目

发布于 2024-12-04 21:14:30 字数 1756 浏览 0 评论 0原文

我正在开发一个带有 razor 的 asp.net mvc3 演示应用程序,该应用程序托管在 http://portfolio-6 上.apphb.com/Projects

我有 mvc 脚手架、Poco 对象作为实体,并且 mvc 脚手架正在运行。

public class Project
{
    public int ProjectId { get; set; }
    [Required(ErrorMessage="please enter name")]
    public string Name { get; set; }
    public string Url { get; set; }
    public DateTime CreatedOn { get; set; }
    public DateTime UpdatedOn { get; set; }
    public bool isFeatured { get; set; }
    public bool isDisabled { get; set; }
    public int GroupId { get; set; }

    public virtual Group Group { get; set; }

    [Required(ErrorMessage="Please select atleast one tag")]
    public virtual ICollection<Tag> Tags { get; set; }
}

public class Tag
{
    public int TagId { get; set; }
    public string Name { get; set; }
    public DateTime CreatedOn { get; set; }
    public DateTime UpdatedOn { get; set; }
    public virtual ICollection<Project> Projects { get; set; }
}
public class Group
{
    public int GroupId { get; set; }
    public string Name { get; set; }
    public DateTime CreatedOn { get; set; }
    public DateTime UpdatedOn { get; set; }

    public virtual ICollection<Project> Projects { get; set; }
}

第一个问题 从上面的代码您可以看到项目和标签实体之间有多对多的关系。 现在我已经设法实现新项目功能,但有一个错误,即项目的标签属性标记为必需,但如果您不选择任何标签,则客户端站点验证将不起作用。

第二个问题 使用多个标签创建项目后,如果您决定编辑它,如何显示与该项目关联的所选标签???

您可以在 https://github.com/najamsk/PortfolioManger 找到完整的源代码

在此处输入图像描述

在此输入图像描述

I am working on a asp.net mvc3 demo app with razor that is hosted on http://portfolio-6.apphb.com/Projects

I have mvc scaffolding, Poco objects as entities and mvc scaffolding running.

public class Project
{
    public int ProjectId { get; set; }
    [Required(ErrorMessage="please enter name")]
    public string Name { get; set; }
    public string Url { get; set; }
    public DateTime CreatedOn { get; set; }
    public DateTime UpdatedOn { get; set; }
    public bool isFeatured { get; set; }
    public bool isDisabled { get; set; }
    public int GroupId { get; set; }

    public virtual Group Group { get; set; }

    [Required(ErrorMessage="Please select atleast one tag")]
    public virtual ICollection<Tag> Tags { get; set; }
}

public class Tag
{
    public int TagId { get; set; }
    public string Name { get; set; }
    public DateTime CreatedOn { get; set; }
    public DateTime UpdatedOn { get; set; }
    public virtual ICollection<Project> Projects { get; set; }
}
public class Group
{
    public int GroupId { get; set; }
    public string Name { get; set; }
    public DateTime CreatedOn { get; set; }
    public DateTime UpdatedOn { get; set; }

    public virtual ICollection<Project> Projects { get; set; }
}

First Problem
From above code you can see I have many to many relation between Project and Tag entities.
Now I have mange to implement New project feature with one bug that is Tags property of Project marked as required but client site validation is not working if you don't select any tag.

Second Problem
Once a project is created with mutliple tags if you decide to edit it how to show the tags selected that are associated with the project???

You can find complete source code at https://github.com/najamsk/PortfolioManger

enter image description here

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

只怪假的太真实 2024-12-11 21:14:30

我已经成功地进行了验证工作,现在在编辑项目时可以选择标签。

但现在我遇到了另一个问题,假设我创建一个带有标签“asp.net”的新项目,后来我编辑项目并仅选择一个标签“wordpress”。现在,如果我发布表单模型绑定有效,但我的上下文无法更新项目的标签集合属性?在我看来,当实体框架为我的项目实体找到复杂类型属性时,它会产生问题吗?

    public void InsertOrUpdate(Project project)
    {
        if (project.ProjectId == default(int)) {
            // New entity
            foreach (var tag in project.Tags)
            {
                context.Entry(tag).State = EntityState.Unchanged;
            }
            context.Projects.Add(project);
        } 
        else {
            // Existing entity
            //every property of project entity is 
            //getting updated only tags property left out :(
            context.Entry(project).State = EntityState.Modified;
        }
    }

I have manage to make validation work and tags are now getting selected while editing a project.

But now I run into another problem lets say I create a new project with tag "asp.net" and later I edit project and choose one tag only "wordpress". Now if i post form model binding works but my context cant update tags collection property of the project? It seems to me it entity framwork making problem here when it finds a complex type property for my Project entity?

    public void InsertOrUpdate(Project project)
    {
        if (project.ProjectId == default(int)) {
            // New entity
            foreach (var tag in project.Tags)
            {
                context.Entry(tag).State = EntityState.Unchanged;
            }
            context.Projects.Add(project);
        } 
        else {
            // Existing entity
            //every property of project entity is 
            //getting updated only tags property left out :(
            context.Entry(project).State = EntityState.Modified;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文