所以我正在尝试建立一个有问题,答案和评论的网站,就像stackoverflow一样。现在,我正在努力让评论正常工作。我已决定尝试使用ActionLink
将评论文本发送到controller
。有没有办法可以在不调用模型字段的情况下执行此操作?
答案 0 :(得分:0)
如果您将输入设置为<form>
代码,该怎么办?
@using (Html.BeginForm("AddComent", "CommentsController"))
{
@Html.TextBox("comment")
<input type="submit" value="Post Your Comment" />
}
P.S。
其他选项(我更喜欢)是使用JQuery发送Ajax Post调用:$.post("@Url.Action("$Action", "$Controller")", { $parameter: $data });
答案 1 :(得分:0)
使用简单的表单提交,然后通过FormCollection
[HttpPost]
public ActionResult SubmitComment(FormCollection collection)
{
string comment = collection["comment"];
}
并为您的观点
@using (Html.BeginForm("SubmitComment", "CommentsController"))
{
@Html.TextBox("comment")
<input type="submit" value="submit" />
}
在我的opninon中,从长远来看,最好将这些数据绑定到模型上。有关详细信息,请参阅此链接:Is there any good reason to use FormCollection instead of ViewModel?