I have been attempting various solutions, but none seem to be working. My goal is to create post and comment partial classes for a main page where end users can add comments. Currently, I am using MVC 5 and the page loads posts and previous comments.
However, I am facing an issue with the onclick method on the client side in order to connect to the server side. I have tried using javascript and ajax to send a call to the C# method location, but the button doesn't respond when clicked. The post partial class is nested within the main page. See code below
Post view
@model IEnumerable<MVC_5_Skeleton1.Models.Post>
<ul class="mylist">
@{
@Html.AntiForgeryToken()
if (Model.Any())
{
foreach (var item in Model)
{
@Html.TextBoxFor(model => item.newComment, "NewComment", htmlAttributes: new { @class = "form-control-inline", placeholder = "Who are you" })
<button type="button" class="btn btn-success btn-block" onclick="AddToCart(@item.newComment)">Post</button>
}
}
Javascript
<script type="text/javascript">
function AddToCart(comment) {
$.ajax({
url: '/Controller/GetTest',
data: { comment: comment },
}).done(function () {
alert('Added');
});
}</script>
Controller...I have tested both with and without HTTPPOST
[HttpPost]
public ActionResult GetTest(String M)
{
return M();
}