views:

97

answers:

0

Hello guys,

I'm trying to write a simple blog post/tag example using dynamic data and a linq data source. I wrote a simple database to store blog posts (title, text) and tags (only a word field and the foreign key) in a sinple one to many relationship and I'd like the user to fill the tags in a single textbox.

So I created a TagString field, marked it as [ScaffoldingColumn], but I can't find a place to update the Tags collection once the user fills in the textbox.

This is what I've tried so far:

  • Updating the Tags collection in the TagString set function, no good. Once it goes to update it either gives me something like "Invalid Operation, can't set post to null on tags" (if I set the Post relationship on the Tag side to not nullable), or "Can't find entity" (otherwise);
  • Using the DetailsView OnUpdating event, the problem here is that the Tags collection comes empty in both the NewObject and OriginalObject arguments.

I'm sure there's no problem in the database or the generated O/R mapping objects, I've made several tests using ordinary forms and datasources and it works just find, I think the problem is the way DynamicData does things.

Does anyone know any way I can do this ?

This is the code I'd like to use for updating the tags, thought it might help to understand the problem (it works fine outside dynamic data):

        IEnumerable<string> words = Regex.Split(_TagString, @"\s+").Distinct();

        var toAdd = words.Where(w => !Tags.Any(t => t.Word == w));
        var toRemove = Tags.Where(t => !words.Contains(t.Word));

        foreach (var item in toRemove)
            Tags.Remove(item);

        Tags.AddRange(toAdd.Select(w => new Tag() { Word = w }));