如何将多个标签保存到数据库 [Asp Mvc]

How to Save Multiple Tags to Database [Asp Mvc]

我正在尝试将多个标签保存到我的数据库中。

这是我的 类:

public partial class Book
{
    [Key]
    public int Book_id { get; set; }

    // ...

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

}

public class Tag
{
    public virtual int Id { get; set; }

    public virtual string Name { get; set; }

    public virtual string UrlSlug { get; set; }

    public virtual string Description { get; set; }

    public virtual IList<Book> Books { get; set; }
}

这是我的视图(创建 post 视图):

<div class="form-group">
    @Html.LabelFor(model => model.Tags, htmlAttributes: new { @class = "control-label col-md-2 col-md-offet-3" })
    <div id="tags">
        @Html.TextBoxFor(model => model.Tags, new { htmlAttributes = new { @class = "form-control" } })
    </div>
</div>

这是我的控制器:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create(Book model)
{
    if (ModelState.IsValid)
    {                
        Collection<Tag> postTags = new Collection<Tag>();
        var s = model.Tags.ToString();
        string[] newTags = s.Split(',');

        foreach (var val in newTags)
        {
            Tag iTag = new Tag
            {
                Name = val
            };
            db.Tags.Add(iTag);
            postTags.Add(iTag);
        }

        model.Tags = postTags;
        db.Books.Add(model);
        await db.SaveChangesAsync();
        return RedirectToAction("Index");
    }

    return View(model);
}

当我添加一个新的 post 并检查数据库时,BookTagMap table 运行良好,但标签 table 保存标签名称如下:

System.Collections.Generic.List`1[SmartBookLibrary.Models.Tag]

为什么标签名称被保存为这个特定的字符串?

来自数据库的数据:

Tags - Table
TagBooks - Table

更新:

当我使用 Html 字段并在控制器中获取 id 时,它起作用了,所以现在 html 助手中的问题,我们该如何解决?

  <input type="text" value="" placeholder="Add a tag" id="tagg" name="tagg"/>

控制器:

public async Task<ActionResult> Create(Book model,string tagg)

这一行,

var s = model.Tags.ToString();

您通过在列表上调用 ToString 将您的列表 model.Tags 转换为字符串(只是在下一行将其转换回列表,顺便说一句)。看一下在列表中调用 ToString 时得到的字符串:https://dotnetfiddle.net/c1SHvP

没有理由将 model.Tags 转换为字符串,然后再将其转换回列表。只需在 foreach 中迭代原始 model.Tags。像这样:

foreach (var tag in model.Tags)
{
    Tag iTag = new Tag
    {
        Name = tag.Name
    };
    db.Tags.Add(iTag);
    postTags.Add(iTag);
}

System.Collections.Generic.List`1[SmartBookLibrary.Models.Tag] 是对象名称。不要使用 toString() 函数。

var s = model.Tags;

更新答案:

public class Book
{
    [Key]
    public int Book_id { get; set; }
    public virtual string TagsListing { get; set; }
    public virtual IList<Tag> Tags { get; set; }
}

查看:

        @Html.LabelFor(model => model.TagsListing, htmlAttributes: new { @class = "control-label col-md-2 col-md-offet-3" })
        <div id="tags">
            @Html.TextBoxFor(model => model.TagsListing, new { htmlAttributes = new { @class = "form-control" } })
        </div>

控制器:

        if (ModelState.IsValid)
        {
            Collection<Tag> postTags = new Collection<Tag>();
            var s = book.TagsListing.ToString();
            string[] newTags = s.Split(',');

            foreach (var val in newTags)
            {
                Tag iTag = new Tag
                {
                    Name = val
                };
                db.Tags.Add(iTag);
                postTags.Add(iTag);
            }

            book.Tags = postTags;
            db.Books.Add(book);
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }