I am attempting to implement a tagging system into my asp.net MVC project. When a user edits or adds a task, they can add any amount of tags they want before submitting. I am using the Jquery Tagit plugin, so when a user adds a new tag an input field is created that looks like:

<input type="hidden" style="display:none;" value="tag1" name="Tags[]">

When the user presses the submit button after adding a few tags, the browser sends the following querystring to the server (retrieved via fiddler):

IsNew=True&Id=2222&Title=Test+Title&Description=Test+Description&Tags%5B%5D=Tag1&Tags%5B%5D=blah&Tags%5B%5D=another-tag

Now my viewmodel that I am serializing this data into has the following structure:

public class KnowledgeBaseTaskViewModel
{
    public int Id { get; set; }

    [Required(AllowEmptyStrings=false, ErrorMessage="Task title is required")]
    [StringLength(500)]
    public string Title { get; set; }

    [Required(AllowEmptyStrings=false, ErrorMessage="Task description is required")]
    [StringLength(500)]
    public string Description { get; set; }

    public List<string> Tags { get; set; }
    public bool IsNew { get; set; } // Needed to determine if we are inserting or not
}

Finally my receiving action has the following signature:

    [HttpPost]
    public ActionResult EditTask(KnowledgeBaseTaskViewModel task)

The issue is that my tag list is not serializing correctly, and my List Tags is null. I have looked at various questions on this site on how to serialize arrays but I still cannot see what I am doing wrong. Any help is greatly appreciated.

link|flag

1 Answer

up vote 1 down vote accepted

It sounds like what you've got should work, but try changing the type of Tags property from List to IList. the model binder might not be using the concrete List<> type.

also, check out this article by Phil Haack: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

link|flag
IList<string> doesn't appear to be binding either :-/ – KallDrexx Jul 23 at 13:58
I checked out the link. Everything I'm doing seems to be in line with what they are saying to do. – KallDrexx Jul 23 at 14:34
remove the square brackets from the name="Tags[]" – dave thieben Jul 23 at 19:08
Interesting idea. I'll give that a try first thing when I get back to work on Monday, thanks! – KallDrexx Jul 25 at 2:19
Removing the brackets worked! Thanks :) – KallDrexx Jul 26 at 14:05

Your Answer

 
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.