0

I'm doing a simple music collection, whe're I have The following classes;

public class Album {

    public int Id { get; set; }
    public string Title { get; set; }
    public string ImageName { get; set; }
    public AlbumType AlbumType { get; set; }
    public int ArtistId { get; set; }
    public virtual Artist Artist { get; set; }
    public virtual ICollection<Track> Tracks { get; set; }
}

public class Track {

    public int Id { get; set; }
    public string Title { get; set; }
    public int RunningTime { get; set; }
    public int AlbumId { get; set; }
    public virtual Album Album { get; set; }
}

I would like to add x number of tracks to an albyum, when I'm creating the album. I suppose you could do it with jQuery, where you would add the textboxes for an additional track, if the first one was filled. I haven't got a clue as to how you would create entire objects dynamically using jQuery - I've done it prevoius with just a single textbox, and added that value as a string. I'm using MVC5 and Visual Studio 2013

EDIT:

This is my View;

<h2>CreateAlbum</h2>


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Album</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ImageName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ImageName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ImageName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.AlbumType, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EnumDropDownListFor(model => model.AlbumType, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.AlbumType, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ArtistId, "ArtistId", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("ArtistId", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.ArtistId, "", new { @class = "text-danger" })
            </div>
        </div>
    </div>
}

I'd like to somehow add a textfield, specifying the number of tracks to add, and the create a series of textboxes I could model bind to the collection on the Album model

catu
  • 888
  • 6
  • 24
  • 1
    Some options for dynamically adding complex types to a collection are [here](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) and [here](http://stackoverflow.com/questions/29161481/post-a-form-array-without-successful/29161796#29161796) –  Mar 22 '15 at 22:00
  • Nice one mate. Thank you very much. BeginCollectionItem was perfect for my needs – catu Mar 23 '15 at 07:31

0 Answers0