许多到许多MVC和EF关系 [英] many-to-many relationship in mvc and EF

查看:206
本文介绍了许多到许多MVC和EF关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个博客网站,用户可以在发表博客,使用实体框架我的模型和数据库存在许多一对多的关系帖子标签

I have created a blog site where users can Post a blog, in my model and database using Entity Framework there is a many-to-many relationship between Posts and Tags.

在我的数据库我也有 PostTagMap ,但是我不认为这在我的EF模型。不知道如果我需要。在 PostTagMap 只包含两个邮政和标签没有其他的唯一标识符。

In my database I also have PostTagMap, however I don't see this in my EF Model. Not sure if I need to. The PostTagMap just contains the unique identifier for both Post and Tag nothing else.

(应注意我创建从模型数据库)

(should be noted I created the database from the model)

在我的创建帖子视图(和控制器)我希望用户必须创建标记(其中有一起发布)的选项确保它们与一个确定另一个。不过,我不能找到一个方法来做到这一点。

On my Create Post View (and controller) I want the users to have the option to create tags (along with there post) making sure that they are identified with one another. However I cant find a way to do this.

邮政型号:

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>

编辑:

有没有办法对我来说,在我的创建查看标签为使用 EditorFor 字段。这是因为创建视图势必 @Model MyBlogger.Post

There is no way for me to use a EditorFor field in my Create View for tags. This is because the Create view is bound to @Model MyBlogger.Post.

标签只显示为一个集合,因此,如果我说:

Tags only show up as a collection and as such if I say:

@Html.EditorFor(model => m.Tags) 

然后渲染时它显示在我看来空白。

Then it shows blank in my view when rendered.

我已经尝试了的foreach 但是,没有工作。例如,在尝试这种我的创建查看:

I have tried a foreach however that did not work. For Instance trying this in my Create view:

@foreach (var item in Model.Tags)
{
    @Html.EditorFor(model => item.Name)
    @Html.EditorFor(model => item.Description)
    @Html.EditorFor(model => item.UrlSlug)
}

我得到的错误:对象引用不设置到对象的实例

推荐答案

许多一对多的关系,如果你使用的是 DataAnnotation EF将创建多到多关系。你的情况

In a Many-to-Many relationship if you are using DataAnnotation EF will create the Many-to-Many relationship. in your case

发表类有一个集合导航属性代码

Post class has a collection navigation property for Tag

public virtual ICollection<Tag> Tags { get; set; }

代码有一个集合的导航属性的发布

And Tag has a collection navigation property for Post

 public virtual ICollection<Post> Posts { get; set; }

这将创造岗位和标签之间的多对一一对多的关系。

Which will create a Many-to-Many relationship between Post and Tag.

有两种方法来添加/获取标签ID的价值,一种方法将其绑定到模型邮政是尝试以下,因为你已经有两个模型之间的关系:

There are couple of ways to add/get the value of the Tag ID , one way to bind it to the model Post is to try the following , since you already have a relationship between the two models:

      <div class="editor-field">
            @Html.EditorFor(model => model.Tags.Id)
            @Html.ValidationMessageFor(model => model.Tags.Id)
        </div>

model.Tags 返回集合的标签如果您需要您需要明确指定它(如身份证,名称的属性.. 。)

model.Tags returns a collection of Tags if you need an attribute you need to specify it explicitly (such as Id, Name ...)

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

这篇关于许多到许多MVC和EF关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆