mvc 4 MultiSelect list& EF很多到很多 [英] mvc 4 MultiSelect list & EF many to many

查看:154
本文介绍了mvc 4 MultiSelect list& EF很多到很多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这些实体(它们之间有很多到很多的连接):

I have these entities (there is many to many connection between them):

public class Post
{     
    public Guid PostId { get; set; }     
    public string Name { get; set; }       
    public virtual ICollection<Tag> Tags { get; set; }
}

  public class Tag
{
    public int TagId { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Post> Posts { get; set; }
}

我想要用户,当他创建Post时,选择数据MultiSelectList和MultiSelectList将该数据传递给Post.Tags。我如何做到这一点?

I'd like user, when he creates Post, to choose data from MultiSelectList and MultiSelectList to pass that data to Post.Tags. How can I do that ?

推荐答案

我有类似的东西,产品属于许多类别和类别,有很多产品。

I have something similar, a Product belongs to many Categories and a Category, has many products.

在我创建新产品的管理视图中,我可以允许用户选择该产品应列出的类别的多个标签。

In my administrative view for creating new products, I am able to allow the user to select mutliple "tags" of categories that this product should be listed under.

由于这么多类别,我倾向于避免多个选择列表,并使用一种自动建议与ajax来检索类别并使用诸如TagIt之类的jQuery插件进行填充。

Because of so many categories I tend to avoid multi select lists and use a sort of auto suggest with ajax to retrieve categories and populate them using a jQuery plugin such as TagIt.

但是为了简单起见,您可以在控制器中使用此功能

But for simplicity you can use this in your Controller

public class HomeController : Controller
{
    public ActionResult Create()
    {
        var tags = new List<Tag>()
            {
                new Tag() { TagId = 1, Name = "Planes", Posts = new Collection<Post>() },
                new Tag() { TagId = 2, Name = "Cars", Posts = new Collection<Post>() },
                new Tag() { TagId = 2, Name = "Boats", Posts = new Collection<Post>() }
            };

        ViewBag.MultiSelectTags = new MultiSelectList(tags, "TagId", "Name");

        return View();
    }

    [HttpPost]
    public ActionResult Create(Post post, int[] tags) // Tags is not case-sensative from model binder when html element name="tags" <-- array passed back to controller
    {

        // Find Tag from Database
        // Attach tag entity to Post

        // foreach(var tagId in tags)
        //    var tag = context.Tags.find(tagId)
        //    post.Tags.Add(tag);

        // context.SaveChanges();

        return RedirectToAction("Create");
    }

}

在您的View / Create中。 cshtml

And inside your View/Create.cshtml

@model MvcApplication1.Models.Post

<h2>Create</h2>


@using (Html.BeginForm("Create", "Home", FormMethod.Post))
{
    <label>Name</label>
    @Html.TextBoxFor(model => model.Name)

    <label>Tags For Post</label>
    @Html.ListBox("Tags", (MultiSelectList)ViewBag.MultiSelectTags)

    <input type="submit" value="Submit Post"/>
}

所有选定的标签:

然后当多次选择发回到控制器时在调试中可以看到模型绑定器知道从html元素名称tags发送数组

Then when multiple are selected on posting back to controller you can see in debug that the model binder knows to send array back from html element name "tags"

这篇关于mvc 4 MultiSelect list&amp; EF很多到很多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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