My action method takes a custom C# class - Comment.cs :
public class Comment
{
public int ID {get; set;}
public int CardID {get; set;}
public int UserID {get;set;}
public string Comment {get;set;}
}
In my view I have a form with input fields for each property in the class except for ID (it's generated by the database). I prevent the default behavior on form submission with jquery and use $.post like this -
$('.form').on('submit', function(e) {
e.preventDefault();
var comment = this.Comment.value;
var cardId = this.CardID.value;
var userId = this.UserID.value;
$.post('/Cards/AddComment',
{
Comment: comment,
CardID: cardId,
UserID: userId
});
});
Html -
<form asp-controller="Cards" asp-action="AddComment" id="comment-form">
<input class="form-control" name="Comment" placeholder="Leave a comment.." />
<input class="form-control" name="CardID" type="hidden" />
<input class="form-control" name="UserID" value="@User.Claims.ElementAt(0).Value" type="hidden" />
<input type="submit" />
</form>
action method -
public IActionResult AddComment(Comment comment)
{
// comment.Comment == null
// comment.CardID == null
// comment.UserID == null
}
I'm trying to automagically cast my posted jQuery data to a Comment type but it isn't working.