I am working on the following. When I run the code, I am able to get the data in the correct form from the database via GET method and I am able to display it correctly in the View. However, on editing/changing some data, say Type, and on pressing Save Data, the POST method gets called, but the studentsViewModel value as the parameter comes as null. No idea why.
I have gone through similar questions on the website and other places online. But they are not having what I am exactly looking for. Plus, most of them are using HTMLHelpers.
Here are the code snippets:
Controller
public IActionResult StudentsDetails(int Id)
{
try
{
var typeItems = _context.Type.Where(t => t.IsActive == true).Select(us => new SelectListItem { Value = us.TypeId.ToString(), Text = us.TypeName.ToString() }).ToList();
ViewBag.TypeItems = typeItems;
var studentsData = _context.Students
.Where(r => r.StudentId == Id).ToList();
var studentsViewModelData = new tudentsViewModel();
studentsViewModelData.Students = studentsData;
studentsViewModelData.StudentId = Id;
return View(studentsViewModelData);
}
catch (Exception ex)
{
....
}
....
}
[HttpPost]
public IActionResult StudentsDetails(int Id, StudentsViewModel studentsViewModel)
{
try
{
if(ModelState.IsValid)
{
...
studentsViewModel.StudentId = Id;
....
}
catch(Exception ex)
{
....
}
....
}
}
Details.cshtml
<div class="card-body card-padding">
<form method="post" asp-action="Details" asp-controller="Students" id="studentsForm">
....
<table id="data-table-basic" class="table table-striped table-vmiddle">
@if(Model.Students.Count() != 0)
{
<thead>
<tr>
<th data-column-id="StudentNumber">Student Number</th>
<th data-column-id="Type">Student Type</th>
</tr>
</thead>
}
<tbody>
@for(int count = 0;count < Model.Students.Count; count++)
{
<tr>
<td>
@Model.Students[count].StudentNumber
</td>
<td>
<select asp-for="@Model.Students[count].TypeId" asp-items="@ViewBag.TypeItems" class="chosen" data-placeholder="Select student type" name="selectType" id="selectType">
<option value=""></option>
</select>
</td>
</tr>
}
</tbody>
</table>
<div>
<button type="submit" value="Save">Save Data</button>
</div>
</form>
</div>
Model
public class Students
{
[Key]
public int StudentId { get; set; }
public string StudentNumber { get; set; }
[ForeignKey("Type")]
public int TypeId { get; set; }
public virtual Type Type { get; set; }
}
StudentsViewModel.cs
public class StudentsViewModel
{
public int StudentId { get; set; }
public List<Students> Students { get; set; }
}
EDIT: Adding Type class
Type.cs
public class Type
{
[Key]
public int TypeId { get; set; }
public string TypeName { get; set; }
public bool IsActive { get; set; }
}
[FromBody]attribute in parameter and try again?public IActionResult StudentsDetails(int Id, [FromBody] StudentsViewModel studentsViewModel)- WinSubmit Data, the page says: This page isn't working` and gives me aHTTP ERROR 415. - Dashamlavasp-action="Details"vsasp-action="StudentsDetails". Is this a typo in your question? Otherwise, you could not even reach to the action method. Could you show usStudentsTableclass? - Win