views:

193

answers:

1

Hey folks, I've been learning MVC 2 and I have pretty much everything understood except for the model part of things, I understand what the model is but actually implementing it has me confused.

Here's my situation, I have my DB which has 3 tables;

Ideas - table of ideas Tags - table of tags IdeaTag - link table connecting the above 2 tables via FKs

So when using the Entity Framework (.edmx) designer in VS2010 I get 2 classes created in the Designer, which obviously map to my DB tables and Navigation Properties in Idea for Tags & Idea for Tag.

So this is all fine until I actually try to add tags to an idea, what's the best practise for dealing with Navigation Properties? I wanted to add a Textbox which will then map to the Tags property in the Idea class but I'm unsure how I'd go about this.

Most of the MVC tutorials which discuss EF or Linq to SQL are quite basic.

If there are any suggestions for tutorials or video tutorials which discuss dealing Navigation Properties and how best to deal with them I'd gladly take it on board. Alternatively if there is a better way to implement this I'd happily look into that too.

A: 

Seeing as this is basically a Many to Many relationship, check out the accepted answer on this question.

It covers the scenario you are looking for. In your case 'ID' is probably the actual tag, i.e. "Entity Framework" or something.

Using the approach in that sample you can have a textbox, with a comma (or something else) delimited list of tag names. Splitting the contents of the textbox gives you an array of tags that should be in the Idea.Tags collection after you're done.

The only complication you have is I guess, you want to automatically create 'new' tags. So perhaps you'll probably need to check whether the Tag exists before attaching it (to add it to the Idea.Tags collection), if it doesn't exist instead of attaching it you would simply add it, so the new tag gets inserted.

Alex James
Thanks for the quick response, although I am unsure as to how to go about binding a textbox to the Tags property, I suppose it wouldn't be something like <%: Html.TextBoxFor(model => model.Tags) %>, but rather a TextBox on it's own which I then get the data from via a FormCollection, correct?
J Pollock