В моем приложении NET Core MVC есть форма для создания проектов (см. Модель ниже). В пределах формы пользователи должны иметь возможность динамически добавлять любое количество требований в список ProjectRequirements.
В настоящее время, когда пользователи добавляют требование, оно сохраняется в таблице с помощью Javascript. Я пытаюсь найти способ динамически отобразить эти значения в мою модель.
Как этого достичь?
Javascript и HTML (сценарий находится на внизу):
<form asp-action="Create">
<h6>Requirements</h6>
<table class="table" id="AddRequirementsTable">
<tr>
<th>Name</th>
<th>Description</th>
<th>Priority</th>
</tr>
@{var count = 0;}
@for (int i = 0; i < count; i++)
{
<tr>
<td contenteditable="true">
<input id="Text1" type="text" asp-for="@Model.ProjectRequirements[i].Name" />
</td>
<td contenteditable="true">
<input id="Text2" type="text" asp-for="@Model.ProjectRequirements[i].Description" />
</td>
<td contenteditable="true">
<input id="Text3" type="text" asp-for="@Model.ProjectRequirements[i].Priority" />
</td>
</tr>
}
</table>
<div class="modal fade" id="new-requirement-modal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Add a New Requirement</h5>
<button type="button" class="close btn btn-round" data-dismiss="modal" aria-label="Close">
<i class="material-icons">close</i>
</button>
</div>
<!--end of modal head-->
<div class="modal-body">
<div>
<div class="tab-content">
<h6>Requirement Details</h6>
<div class="form-group row">
<label class="col-3">Title</label>
<input class="form-control col" placeholder="Requirement title" id="title" />
</div>
<div class="form-group row align-items-center">
<label class="col-3">Description</label>
<textarea class="form-control col" rows="3" placeholder="Requirement description" id="description"></textarea>
</div>
<div class="form-group row align-items-center">
<label class="col-3">Priority</label>
<select asp-items="Html.GetEnumSelectList<Priority>()" class="form-control col-3" id="priority">
<option value=""></option>
</select>
</div>
</div>
</div>
</div>
<!--end of modal body-->
<div class="modal-footer">
<button role="button" type="button" class="btn btn-primary" onclick="addRow()" data-dismiss="modal" @{count++;}>
Add requirement
</button>
</div>
</div>
</div>
</div>
<br />
<button role="button" type="button" class="btn btn-outline-primary" data-toggle="modal" data-target="#new-requirement-modal">
New Requirement
</button>
</div>
</div>
</div>
<!--end of modal body-->
<div class="modal-footer">
<button role="button" class="btn btn-primary" type="submit">
Create Project
</button>
</div>
</form>
@section Scripts {
@{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
<script src="~/js/custom-validation.js"></script>
<script>
function addRow() {
var table = document.getElementById("AddRequirementsTable");
var row = table.insertRow(-1);
var cell1 = row.insertCell(0);
cell1.innerHTML = document.getElementById("title").value;
var cell2 = row.insertCell(1);
cell2.innerHTML = document.getElementById("description").value;
var cell3 = row.insertCell(2);
cell3.innerHTML = document.getElementById("priority").value;
}
</script>
}
ProjectCreateViewModel:
public class ProjectCreateViewModel
{
public int ProjectId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public List<ProjectRequirement> ProjectRequirements { get; set; }
public ICollection<ProjectTask> Tasks { get; set; }
public Priority Priority { get; set; }
public Status Status { get; set; }
public bool AwardEligibility { get; set; }
public decimal Progress { get; set; }
public List<ProjectUsersViewModel> Users { get; set; }
public DateTime StartDate { get; set; }
public DateTime Deadline { get; set; }
public DateTime EstimatedCompletionDate { get; set; }
public List<ProjectUsersViewModel> AllUsers { get; set; }
}
}
Форма состоит из 3 вкладок, одной из которых является «Требования» , На этой вкладке я создал кнопку «Новое требование», которая открывает модальное окно с формой внутри, чтобы добавить новое требование в форму (сопоставленное со списком ProjectRequirements).