[英]Post a comment from HTML to C# model in a list using MVC
我尝试了一些修复程序,但似乎不合适。 我正在创建一个帖子并评论进入主页的部分类。 我想允许最终用户添加评论。 我正在使用MVC 5,目前该页面加载了帖子和较早的评论。
但是,我试图在客户端使用onclick方法连接到我的服务器端。 然后在javascript中,我尝试使用对ajax的调用将调用发送到c#方法的位置。 该按钮似乎甚至没有意识到它已被单击。 我在主页上嵌套了部分子类。 下面的代码
帖子视图
@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>
}
}
Java脚本
<script type="text/javascript">
function AddToCart(comment) {
$.ajax({
url: '/Controller/GetTest',
data: { comment: comment },
}).done(function () {
alert('Added');
});
}</script>
控制器...我尝试过有无HTTPPOST
[HttpPost]
public ActionResult GetTest(String M)
{
return M();
}
在您的ajax函数中,添加此参数
type: "POST",
并通过(string comment)
在控制器操作中重命名(string M)
(string comment)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.