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

时间:2016-02-09 18:35:35

标签: asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 razor

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

以下是我的课程:

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

这是我的观点(创建帖子视图):

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

当我添加新帖子并检查数据库时,BookTagMap表运行良好,但tags表保存了Tag Name,如下所示:

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

为什么标签名称会保存为此特定字符串?

数据库中的数据:

this msdn-article
Tags - Table

已更新:

当我使用Html字段,并获得控制器中的id时,它是有效的,所以现在html helper中的问题,我们如何解决它?

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

控制器:

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

2 个答案:

答案 0 :(得分:0)

在这一行中,

var s = model.Tags.ToString();

通过调用列表中的model.Tags将您的列表ToString转换为字符串(仅将其转回到下一行的列表中,顺便说一下)。看看您在列表中ToString

上拨打model.Tags时获得的字符串

没有理由将model.Tags转换为字符串然后将其转回列表。只需迭代foreach中的原始foreach (var tag in model.Tags) { Tag iTag = new Tag { Name = tag.Name }; db.Tags.Add(iTag); postTags.Add(iTag); } 即可。像这样:

@Html.Hidden("MyURL", @Url.Action("Lite", "Service"))

答案 1 :(得分:0)

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