I am working with Asp.Net MVC, and I have a DTO that looks like this:
public class TaskDTO
{
public string TaskName { get; set; }
public string NextTaskName { get; set; }
public bool IsBasicTask { get; set; }
public int EstimatedTime { get; set; }
public List<ResourceTaskDTO> RequiredResources { get; set; }
}
public class ResourceTaskDTO
{
public string ResourceName { get; set; }
public int Id { get; set; }
public int Count { get; set; }
}
What I am trying to do, is that on the view side of this, I want to have forms and tables to fill up the TaskDTO. On the view side, I have the normal forms, and also a table that is populated by javascript to add the ResourceTaskDTO.
@model CMBuilder.Models.Api.TaskDTO
@{
ViewData["Title"] = "Create";
}
<h1>Create</h1>
<h4>Task</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="TaskName" class="control-label"></label>
<input asp-for="TaskName" class="form-control" />
<span asp-validation-for="TaskName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="NextTaskName" class="control-label"></label>
<input asp-for="NextTaskName" class="form-control" />
<span asp-validation-for="NextTaskName" class="text-danger"></span>
</div>
<div class="form-group form-check">
<label class="form-check-label">
<input class="form-check-input" asp-for="IsBasicTask" /> @Html.DisplayNameFor(model => model.IsBasicTask)
</label>
</div>
<div class="form-group">
<label asp-for="EstimatedTime" class="control-label"></label>
<input asp-for="EstimatedTime" class="form-control" />
<span asp-validation-for="EstimatedTime" class="text-danger"></span>
</div>
<div class="form-group">
<table class="table table-bordered" id="ResourceTable">
<tr>
<th>Resource Name</th>
<th>Id</th>
<th>Count</th>
<th><button type="button" name="add" id="btn_AddResource" class="btn btn-success btn-sm add"><span class="glyphicon glyphicon-plus"></span></button></th>
</tr>
</table>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
@section Scripts
{
<script>
i = 0;
$("#btn_AddResource").click(function () {
var html = '';
html += '<tr>';
html += '<td><input type="text" asp-for="RequiredResources[i].ResourceName" class="form-control" /></td>';
html += '<td><input type="text" asp-for="RequiredResources[i].Id" class="form-control" /></label></td>';
html += '<td><input type="text" asp-for="RequiredResources[i].Count" class="form-control" /></td>';
html += '<td></td></tr>';
$('#ResourceTable').append(html);
i++;
});
</script>
}
However, when I access the "RequiredResources" attribute in the ResourceTaskDTO in the controller's "Create" method, it is NULL and it seems that the javascript code did not hook into the "RequiredResources" attribute.
This is my Controller Code:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("TaskName,NextTaskName,IsBasicTask,EstimatedTime,RequiredResources")] TaskDTO task)
{
if (ModelState.IsValid)
{
CMBuilderHandler Handler = new CMBuilderHandler(_service);
var res = await Handler.CreateTask(task);
if(res)
{
return Ok("Success");
}
else
{
return NotFound("Something went bad!");
}
}
return View(task);
}
What is wrong with my code? Why does task.RequiredResources null when I get it back on the controller?