![](/img/trans.png)
[英]Working with EF 6 MVC 5 and retrieving data from many-to-many relationship
[英]many-to-many relationship in mvc and EF
我创建了一个博客网站,用户可以Post
博客,在我的模型和数据库中使用Entity Framework, Posts
和Tags
之间存在多对多的关系。
在我的数据库中我也有PostTagMap
,但是在我的EF模型中没有看到这个。 不确定我是否需要。 PostTagMap
只包含Post和Tag的唯一标识符。
(应该注意我从模型中创建了数据库)
在我的Create Post
视图(和控制器)上,我希望用户可以选择创建标签(以及帖子),确保他们彼此识别。 但是我找不到办法做到这一点。
邮政模式:
public partial class Post
{
public Post()
{
this.Tags = new HashSet<Tag>();
}
public int Id { get; set; }
public string BlogUserEmail { get; set; }
public int CategoryId { get; set; }
public string Title { get; set; }
public string ShortDescription { get; set; }
[AllowHtml]
public string Description { get; set; }
public string Meta { get; set; }
public string UrlSlug { get; set; }
public bool Published { get; set; }
public System.DateTime PostedOn { get; set; }
public Nullable<System.DateTime> Modified { get; set; }
public virtual BlogUser BlogUser { get; set; }
public virtual Category Category { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Post</legend>
<div class="editor-label">
@Html.HiddenFor(model => model.BlogUserEmail, User.Identity.Name)
</div>
<div class="editor-field">
@Html.ValidationMessageFor(model => model.BlogUserEmail)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.CategoryId, "Category")
</div>
<div class="editor-field">
@Html.DropDownList("CategoryId", String.Empty)
@Html.ValidationMessageFor(model => model.CategoryId)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Title)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Title)
@Html.ValidationMessageFor(model => model.Title)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ShortDescription)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ShortDescription)
@Html.ValidationMessageFor(model => model.ShortDescription)
</div>
<div class="editor-label">
@Html.TextAreaFor(model => model.Description)
</div>
<div class="editor-field">
@Html.ValidationMessageFor(model => model.Description)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
编辑:
我无法在Create
视图中使用EditorFor
字段来标记。 这是因为Create
视图绑定到@Model MyBlogger.Post
。
标签只显示为集合,如果我说:
@Html.EditorFor(model => m.Tags)
然后它在渲染时在我的视图中显示为空白。
我试过一个foreach
但是没有用。 对于在我的Create
视图中尝试此操作的实例:
@foreach (var item in Model.Tags)
{
@Html.EditorFor(model => item.Name)
@Html.EditorFor(model => item.Description)
@Html.EditorFor(model => item.UrlSlug)
}
我得到错误: Object reference not set to an instance of an object.
EF模型中不显示多对多表。 要向其添加行,您必须创建类似于:PostObject1.Tags.Add(TagObject1)或TagObject1.Posts.Add(PostObject1)。
以下是如何执行此操作: 插入/更新多个到多个实体框架。 我该怎么做?
要选择行,您需要EF对象并访问另一个表的列表。
在Many-to-Many
关系中如果使用DataAnnotation
EF将创建多对多关系。 在你的情况下
Post类具有Tag的集合导航属性
public virtual ICollection<Tag> Tags { get; set; }
And Tag有Post的集合导航属性
public virtual ICollection<Post> Posts { get; set; }
这将在Post和Tag之间创建多对多关系。
有几种方法可以添加/获取标记ID的值,将其绑定到模型的一种方法是尝试以下方法,因为您已经在两个模型之间建立了关系:
<div class="editor-field">
@Html.EditorFor(model => model.Tags.Id)
@Html.ValidationMessageFor(model => model.Tags.Id)
</div>
如果你需要一个你需要明确指定它的属性(例如Id,Name ......), model.Tags
返回一个Tags集合
if (ModelState.IsValid)
{
Tag tag = new tag{id=post.Tags.Id};
db.Tag.Attach(tag);
db.Tag.Add(tag);
db.Posts.Add(post);
db.SaveChanges();
return RedirectToAction("Index");
}
关联(映射)表可能不会显示在EF模型中,但您可以轻松添加新标记
post.Tags.add(new Tag(){ Name = ""});
作为一种好的做法您应该创建视图模型而不是直接使用模型进行视图,
这是帮助您链接的链接
对于标签,您可以执行EditorTemplate(推荐)或For循环
这是for循环示例
@for(var i=0;i<Model.Tags.Count();i++) { @Html.EditorFor(model => model.Tags[i].Name) }
对于新建
只需Add New
按钮,然后onclick
,只需添加一个新文本框,但将Id设置为Tags[i].Name
,其中i是Tags[i].Name
的Count()+1
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.