Create list of entities in asp.net core mvc

僤鯓⒐⒋嵵緔 提交于 2019-12-03 09:03:12

Remove the [Bind("Id,Date,Description,Name")] from your action method.

public async Task<IActionResult> Create(Workout workout)
{
    // do stuff
}

Then apply indexing on your Set properties like this.

<div class="form-inline add-set">
                                <div class="form-group">
                                    <label class="control-label">Exercise</label>
                                    <select name="Sets[0].ExerciseId" class="form-control"><option value="1">Push Up</option>
    <option value="1">Set Up</option>
    </select>

<div class="add-sets">
    <div class="form-inline add-set">
        <div class="form-group">
            <label class="control-label">Exercise</label>
            <select name="Sets[0].ExerciseId" class="form-control" asp-items="ViewBag.Exercises"></select>
        </div>
        <div class="form-group">
            <label asp-for="Sets.First().Reps" class="control-label"></label>
            <input asp-for="Sets.First().Reps" name="Sets[0].Reps" placeholder="Reps" class="form-control" />
            <span asp-validation-for="Sets.First().Reps" class="text-danger" />
        </div>
        <div class="form-group">
            <label asp-for="Sets.First().Weight" class="control-label"></label>
            <input asp-for="Sets.First().Weight" name="Workout.Sets[0].Weight" class="form-control" />
            <span asp-validation-for="Sets.First().Weight" class="text-danger" />
        </div>
        <div class="form-group">
            <button class="btn btn-remove-set" data-toggle="tooltip" title="Remove Set"><span class="glyphicon glyphicon-minus"></span></button>
        </div>
    </div>
</div>

When you add new row dynamically make sure the indexing number must be sequential, start at 0 and not skip any iteration.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!