Mvc3 列表框用于在编辑实体时辅助方法选择项目
我正在开发一个带有 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经成功地进行了验证工作,现在在编辑项目时可以选择标签。
但现在我遇到了另一个问题,假设我创建一个带有标签“asp.net”的新项目,后来我编辑项目并仅选择一个标签“wordpress”。现在,如果我发布表单模型绑定有效,但我的上下文无法更新项目的标签集合属性?在我看来,当实体框架为我的项目实体找到复杂类型属性时,它会产生问题吗?
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?