Asp.net mvc 3 Asp.net MVC 3多对多关系使用jquery autocomplete创建新项
我是asp.NETMVC新手,正在尝试创建一个简单的网站。有一个贴子的标签具有多对多关系。我已经阅读了关于如何做多对多的教程,但我不理解。我想做的就是在创建一篇新文章时,能够用关键字标记文章,就像在这个网站上创建一篇文章一样(stackoverflow)。我想让jquery在Tags文本框中自动选择,然后在创建项时在join-the表中创建相应的条目 因此,我的问题是:Asp.net mvc 3 Asp.net MVC 3多对多关系使用jquery autocomplete创建新项,asp.net-mvc-3,many-to-many,jquery-autocomplete,Asp.net Mvc 3,Many To Many,Jquery Autocomplete,我是asp.NETMVC新手,正在尝试创建一个简单的网站。有一个贴子的标签具有多对多关系。我已经阅读了关于如何做多对多的教程,但我不理解。我想做的就是在创建一篇新文章时,能够用关键字标记文章,就像在这个网站上创建一篇文章一样(stackoverflow)。我想让jquery在Tags文本框中自动选择,然后在创建项时在join-the表中创建相应的条目 因此,我的问题是: 如何在标记文本字段上设置jquery自动完成 Ho使用所选标记创建新帖子(在联接表中) 我看到了几个示例,其中有多对多创建,您
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content {get; set;}
public virtual ICollection<Tag> Tags { get; set; }
}
public class Tag
{
public int TagId { get; set; }
public int Title { get; set; }
public virtual ICollection<Post> Posts { get; set; }
}
public class PostEntities : DbContext
{
public DbSet<Post> Posts { get; set; }
public DbSet<Tag> Tags { get; set; }
}
剃刀:
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Post</legend>
<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.Content)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Content)
@Html.ValidationMessageFor(model => model.Content)
</div>
<div>
<h3>Tags</h3>
@Html.TextBoxFor(model => model.Tags, new { @id = "NewTags" })
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
@使用(Html.BeginForm()){
@Html.ValidationSummary(true)
邮递
@LabelFor(model=>model.Title)
@EditorFor(model=>model.Title)
@Html.ValidationMessageFor(model=>model.Title)
@LabelFor(model=>model.Content)
@EditorFor(model=>model.Content)
@Html.ValidationMessageFor(model=>model.Content)
标签
@Html.TextBoxFor(model=>model.Tags,新的{@id=“NewTags”})
}如果您想将文本框用作多选项,您需要一个类似jquery的插件。然后,您可以使用如下视图模型:
public class PostViewModel
{
public Post Post { get; set;}
public virtual ICollection<Tag> Tags { get; set; }
public virtual ICollection<Tag> relatedTags { get; set; }
//in the constructor you can initialize the list
// with all the tags available in your system
public PostViewModel()
{
foreach (var tagin new dbContext().Tags.GetList())
{
Tags.Add(tag);
}
}
其中,id“relatedTagsToPost”的选择通过以下方式初始化:
$("#relatedTagsToPost").chosen();
仅此而已,它并没有经过测试,只是根据我的项目和你的问题进行了调整。如果你想将文本框用作多选项,你需要一个类似jquery的插件。然后,您可以使用如下视图模型:
public class PostViewModel
{
public Post Post { get; set;}
public virtual ICollection<Tag> Tags { get; set; }
public virtual ICollection<Tag> relatedTags { get; set; }
//in the constructor you can initialize the list
// with all the tags available in your system
public PostViewModel()
{
foreach (var tagin new dbContext().Tags.GetList())
{
Tags.Add(tag);
}
}
其中,id“relatedTagsToPost”的选择通过以下方式初始化:
$("#relatedTagsToPost").chosen();
仅此而已,它并没有经过测试,只是根据我的项目和您的问题进行了调整。Vic的答案是一个很好的解决方案,但我更喜欢Autocomplete jQuery插件,因为您还可以添加新标签,而不仅仅是从预定义或远程加载的列表中选择标签 看看:
然后,您必须编辑一点控制器的代码,将输入标记按“,”分开,然后保存到数据库中。Vic的答案是一个很好的解决方案,但我更喜欢Autocomplete jQuery插件,因为您还可以添加新标记,而不仅仅是从预定义或远程加载的列表中选择标记 看看: 然后,您必须编辑一点控制器的代码,按“,”拆分输入标记,然后将它们保存在数据库中
$("#relatedTagsToPost").chosen();