2017-01-04 43 views
1

我想在asp.net核心mvc中創建一個實體。實體表示了鍛鍊,它具有的運動集的子集:在asp.net核心mvc中創建實體列表

public class Workout 
{ 
    public DateTime Date { get; set; }   
    public string Name { get; set; } 
    public string Description { get; set; } 

    public virtual ICollection<Set> Sets { get; set; } 
} 

public class Set 
{ 
    public int Id { get; set; } 
    public int ExerciseId { get; set; } 
    public int WorkoutId { get; set; } 
    public decimal Weight { get; set; } 
    public int Reps { get; set; } 

    public virtual Exercise Exercise { get; set; } 
    public virtual Workout Workout { get; set; } 
} 

的形式如下:

enter image description here

「添加設置」按鈕,使用JavaScript來添加將「設置」相關字段組成表格,所以可以有任意數量的「設置」相關字段。

這裏是我現有的包含表單視圖代碼:

@model Weightlifting.Models.Workout 

@{ 
    ViewData["Title"] = "Add Workout"; 
} 

<h2>Add Workout</h2> 

<form asp-action="Create"> 
    <div class="form-horizontal"> 
     <div asp-validation-summary="ModelOnly" class="text-danger"></div> 
     <div class="form-group"> 
      <label asp-for="Date" class="col-md-2 control-label"></label> 
      <div class="col-md-10"> 
       <input asp-for="Date" class="form-control" /> 
       <span asp-validation-for="Date" class="text-danger" /> 
      </div> 
     </div> 
     <div class="form-group"> 
      <label asp-for="Name" class="col-md-2 control-label"></label> 
      <div class="col-md-10"> 
       <input asp-for="Name" class="form-control" /> 
       <span asp-validation-for="Name" class="text-danger" /> 
      </div> 
     </div> 
     <div class="form-group"> 
      <label asp-for="Description" class="col-md-2 control-label"></label> 
      <div class="col-md-10"> 
       <input asp-for="Description" class="form-control" /> 
       <span asp-validation-for="Description" class="text-danger" /> 
      </div> 
     </div>   
     <div class="form-group"> 
      <fieldset class="form-add-set"> 
       <label for="sets" class="col-md-2 control-label">Sets</label> 
       <div class="col-md-10"> 
        <div class="add-sets"> 
         <div class="form-inline add-set"> 
          <div class="form-group"> 
           <label class="control-label">Exercise</label> 
           <select 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" 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" 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> 
        <button class="btn btn-add-set">Add Set</button> 
       </div> 
      </fieldset> 
     </div> 
     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Save" class="btn btn-primary" /> 
      </div> 
     </div> 
    </div> 
</form> 

<div> 
    <a asp-action="Index"><span class="glyphicon glyphicon-arrow-left"></span> Back</a> 
</div> 

@section Scripts { 
<script type="text/javascript"> 
    $(document).ready(function() {  
     var wrapper = $('.add-sets'); 

     $(".btn-add-set").click(function(e) { 
      e.preventDefault(); 
      $('.add-set:first-child').clone(true).appendTo(wrapper); 

      $('.add-set .btn-remove-set').show(); 
     }); 

     $('.btn-remove-set').click(function (e) { 
      e.preventDefault(); 
      $(this).parents('.add-set').remove(); 

      removeButton(); 
     }); 

     function removeButton() { 
      if ($('.add-set').length == 1) { 
       $('.add-set .btn-remove-set').hide(); 
      } 
     } 
    }); 
</script> 
} 

這裏是控制器動作,視圖員額:

public async Task<IActionResult> Create([Bind("Id,Date,Description,Name")] Workout workout) 
{ 
    // do stuff 
} 

我的問題是:我如何對錶單進行編碼以便將「集合」發佈到鍛鍊模型中的集合集合?

回答

1

從您的操作方法中刪除[Bind("Id,Date,Description,Name")]

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

然後在你的Set這個屬性上應用索引。

<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> 

當您動態添加新行時,請確保索引編號必須是連續的,從0開始並且不跳過任何迭代。

+0

甜。這是完美的。 – clhereistian