I'm developing a simple blog application where users submitting a new post can also choose to associate a selection of predefined tags to their Post.
Currently, users can select multiple tags (holding ctrl) using a dropdownlist:
However, aesthetically this is not really what I want to use for this purpose and would prefer to let users select desired tags from a series of checkboxes, or even better - buttons. Similar to this:
Does such an equivalent HTML helper exist?
My working code with dropdownlist is below for your reference.
VIEW:
<div class="form-group">
@Html.DropDownList("TagId", (MultiSelectList)ViewBag.Tags, new { multiple = "multiple" })
</div>
CONTROLLER:
_context is my database containing two tables, Post and Tags.
public ActionResult Add()
{
var tags = _context.Tags.Select(c => new {
TagID = c.TagID,
Type = c.Type
}).ToList();
ViewBag.Tags= new MultiSelectList(tags, "TagID", "Type");
return View();
}
[AcceptVerbs("POST")]
public ActionResult Add()
{
string TagId= Request["TagID"];
var tagids = TagId.Split(',');
foreach (var tagid in tagids)
{
var id = int.Parse(tagid);
Tag tag = _context.Tags.Where(ct => ct.TagId== id).First();
post.Tags.Add(tag);
}
//Save our post
_context.Post.Add(post);
_context.SaveChanges();
return View();
}
Any alternative suggestion are also highly appreciated, particularly as I'm still learning the ropes - thanks!