从文本框中获取非模型信息,并通过Actionlink进行传递

本文关键字:Actionlink 文本 获取 信息 模型 | 更新日期: 2023-09-27 17:59:54

所以我正试图建立一个有问题、答案和评论的网站,就像stackoverflow一样。现在,我正在努力让评论正确工作。我决定尝试使用ActionLink将注释文本发送到controller。有没有什么方法可以在不调用模型字段的情况下做到这一点?

从文本框中获取非模型信息,并通过Actionlink进行传递

使用简单的表单提交,然后通过FormCollection 获取提交的数据

[HttpPost] 
public ActionResult SubmitComment(FormCollection collection)
{
   string comment = collection["comment"];
}

和您的看法

@using (Html.BeginForm("SubmitComment", "CommentsController"))
{
   @Html.TextBox("comment")
   <input type="submit" value="submit" />
}

在我的观点中,从长远来看,您最好将这些数据绑定到模型。有关更多详细信息,请参阅此链接:是否有充分的理由使用FormCollection而不是ViewModel?

如果您将输入放置到<form>标记中会怎样?

@using (Html.BeginForm("AddComent", "CommentsController"))
{
   @Html.TextBox("comment")
   <input type="submit" value="Post Your Comment" />
}

第页。S另一个选项(我更喜欢)是使用JQuery:$.post("@Url.Action("$Action", "$Controller")", { $parameter: $data });

发送Ajax Post调用