I am new to ASP.NET MVC, so apologies if this is easier than it looks. I've been Googling around, I have a class like so:
public class Search : IAuditable
{
[Key]
public int SearchID { get; set; }
public int UserID { get; set; }
...
public ICollection<SearchTerm> SearchTerms { get; set; }
}
In the Create.cshtml, I have the following
<div class="editor-label">
@Html.LabelFor(model => model.Search.SearchTerms)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Search.SearchTerms, "SearchTerm")
@Html.ValidationMessageFor(model => model.Search.SearchTerms)
</div>
SearchTerm EditorTemplate is a simple form like so:
@model Core.Search.Parameters.SearchTerm
@Html.HiddenFor(n => n.SearchTermID)
<div class="editor-label">
@Html.LabelFor(n => n.Text, "Term")
</div>
<div class="editor-field">
@Html.EditorFor(n => n.Text)
@Html.ValidationMessageFor(n => n.Text)
</div>
It seems to work, I see a single text box on create (when the default model is empty). However, what I want to do is be able to add / remove the SearchTerms
with an Add button so the user can add an arbitrary amount of terms to the collection on create. Is this built in somehow? is there a javascript framework that pairs well with this, that will generate the appropriate html names so I don't have to manually do that? Am I even approaching this the right way, given that I am new to MVC?
Thanks!