0

I have to create a notes app for homework and it should contain Notes with a List of Tags as property. My generated Notes object looks like this:

public partial class Note
{
    public Note()
    {
        this.Tags = new HashSet<Tag>();
    }

    public int Id { get; set; }
    public string Title { get; set; }
    public string Text { get; set; }
    public string Author { get; set; }
    public string Mail { get; set; }

    public virtual ICollection<Tag> Tags { get; set; }
}

Also I have a generated Tag Object:

public partial class Tag
{
    public Tag()
    {
        this.Notes = new HashSet<Note>();
    }

    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Note> Notes { get; set; }
}

This is done with a many to many relationship, i have no entity for NoteTags but a database table. Now how do I make it possible for the user to edit the tags for a note? I tried many things already, for example this but that did not work for me because i would have to edit the generated model.

Idk if this helps to understand my situation, here's my editortemplate tag.cshtml:

@model IEnumerable<CRUDApp.Models.Tag>

@foreach (var item in Model)
{
    @Html.EditorFor(modelItem => item.Name)<br />
}

Whenever I post the form in the edit mode, Tags will be null within the Note object.

Community
  • 1
  • 1
  • Assuming your wanting to dynamically add/remove `Tag`'s in ``Note`, refer the answers [here](http://stackoverflow.com/questions/29161481/post-a-form-array-without-successful/29161796#29161796) and [here](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) for some options –  Feb 10 '16 at 08:48
  • And you cannot use a `foreach` loop to generate form controls for a collection. You need a `for` loop or custom `EditorTemplate` for typeof `Tag` - refer [this answer](http://stackoverflow.com/questions/33984161/get-list-data-on-view-in-controller/34033602#34033602) for more explanation –  Feb 10 '16 at 08:49
  • Whenever I try to access tags by indexer i get an error that this is not possible is there a workaround? – Timo Jokinen Feb 10 '16 at 08:53
  • Because your collection property needs to implement `IList` - as always, use a view model. –  Feb 10 '16 at 08:55

0 Answers0