I have created a form that edits a list of notes. The form displays the Id of each note along with the message it contains. This works.
The problem is that after changing the messages in these notes and submitting the changes, the form is sent but I receive an empty list of models from the HttpPost action-method parameter.
I looked through many similar questions but a common problem was that the view model did not contain public properties. Mine does. I can't see where the problem is. I am a beginner to programming so I apologise if the problem is too obvious.
// My View Model
public class NoteViewModel
{
public int Id { get; set; }
public string Message { get; set; }
}
// My Post Action method
[HttpPost]
public IActionResult EditNotes(List<NoteViewModel> model)
{
foreach (var item in model)
{
// Create a note, and copy values from model
Note note = new Note
{
Id = item.Id,
Message = item.Message
};
// Update note in database.
noteRepository.Update(note);
}
return RedirectToAction("NotePage", "Home");
}
// My View EditNote.cshtml
@model List<MyWebsite.ViewModels.NoteViewModel>
<form asp-action="EditNotes" method="post">
@foreach (var note in Model)
{
<label asp-for="@note.Id">@note.Id</label>
<label asp-for="@note.Message">Message</label>
<input asp-for="@note.Message" value="@note.Message" />
}
<button type="submit" class="btn btn-success">Submit</button>
</form>
I expect to receive a list of models that contain the notes, but I receive an empty list here
public IActionResult EditNotes(List<NoteViewModel> model)
{
// model is empty
// model.Count() gives 0.
}