Javascript 如何在Ajax.ActionLink中以表单形式获取输入的数据?
我的页面很简单。它显示一张图片并有一个注释部分(这是一个局部视图)。我只想在提交新评论时刷新局部视图。所以我有这个:Javascript 如何在Ajax.ActionLink中以表单形式获取输入的数据?,javascript,jquery,ajax,asp.net-mvc,razor,Javascript,Jquery,Ajax,Asp.net Mvc,Razor,我的页面很简单。它显示一张图片并有一个注释部分(这是一个局部视图)。我只想在提交新评论时刷新局部视图。所以我有这个: <tr> <td>@Html.TextBox("NewComment")</td> </tr> <tr> @Ajax.ActionLink("Submit", "InsertComment", new
<tr>
<td>@Html.TextBox("NewComment")</td>
</tr>
<tr>
@Ajax.ActionLink("Submit", "InsertComment", new
{
Id = Model.userParticipation.Id,
CurrentPosition = 0,
CurrentComments = Model.currentComments,
NewCommentText = "???"
},
new AjaxOptions
{
HttpMethod = "GET",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "CommentSection"
})
</tr>
@Html.TextBox(“新成员”)
@ActionLink(“提交”,“插入命令”,新建
{
Id=Model.userParticipation.Id,
CurrentPosition=0,
CurrentComments=Model.CurrentComments,
NewCommentText=“??”
},
新选择
{
HttpMethod=“GET”,
InsertionMode=InsertionMode.Replace,
UpdateTargetId=“CommentSection”
})
我唯一的问题是,我不知道如何将NewComment文本框中输入的文本传递给NewCommentText变量(而不是“?”字符串)。任何帮助都将不胜感激。
@Ajax.ActionLink
将为锚定标记生成标记(连接ajaxified行为)。但是,由于您希望向服务器提交新的注释,因此需要一个输入字段供用户输入注释,并且可能需要一个表单。如果希望此表单提交具有ajaxified行为,可以使用Ajax.BeginForm
helper方法
@using (Ajax.BeginForm("InsertComment", "Home", new
{
Id = Model.userParticipation.Id,
CurrentPosition = 0,
}, new AjaxOptions
{
HttpMethod = "POST",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "CommentSection"
}))
{
<label>Enter comment</label>
<input type="text" name="NewCommentText" />
<input type="submit" />
}
<div id="CommentSection"></div>
您需要的是ajax开始表单,而不是ajax.action linkCool!很高兴我能帮忙。
[HttpPost]
public ActionResult InsertComment(string NewCommentText,int Id,int currentPosition)
{
// to do : Save and return some valid markup
return Content("To do : Replace this with useful html markup");
}