如何在Ajax.ActionLink中获取表单中的输入数据?
My page is quite simple. It shows a picture and has a comment section (which is a partial view). I only want the partial view to refresh whenever a new comment has been submitted. So I have this:
我的页面很简单。它显示了一张图片,并有一个注释部分(即部分视图)。我只希望部分视图在提交新评论时刷新。所以我有这个:
<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>
The only prolem I have is that I don't know how to pass the entered text in the NewComment TextBox to the NewCommentText variable (instead of the "???" string). Any help would be appreciated.
唯一的问题是,我不知道如何将在NewComment文本框中输入的文本传递给NewCommentText变量(而不是“???”字符串)。如有任何帮助,我们将不胜感激。
1 个解决方案
#1
2
@Ajax.ActionLink
is going to generate the markup for an anchor tag (with ajaxified behavior wired in). But since you want to submit a new comment to the server, you need an input field for user to enter the comment and potentially a form. If you want the ajaxified behavior for this form submission, you can use the Ajax.BeginForm
helper method.
@Ajax。ActionLink将为锚标记生成标记(连接到ajaxified behavior)。但是由于您想要向服务器提交一个新的注释,所以您需要一个输入字段来让用户输入评论和潜在的表单。如果您想要这个表单提交的Ajax行为,您可以使用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>
This will generate the form tag with an input element with name attribute value set to "NewCommentText".
这将生成表单标记,其输入元素的名称属性值设置为“NewCommentText”。
This should work assuming your InsertComment
action method (inside HomeController) has a parameter with the same name as the input field, along with the other paramters like Id
and CurrentPosition
.
假设InsertComment操作方法(在HomeController中)有一个与输入字段同名的参数,以及Id和CurrentPosition等其他参数,那么这应该可以工作。
[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");
}
更多相关文章
- 动态添加表单元素,将它们存储到MySQL并在将来更改/删除它们
- Ajax php登录表单不指向另一个页面
- 分享27款非常棒的 jQuery 表单插件
- 如何使用jQuery的叠加对话框作为反馈表单
- 用jQuery编写网页-表单检查
- jquery validate和jquery form 插件组合实现验证表单后AJAX提交
- 为什么在使用jquery读写表单输入时必须对字符串进行编码?
- 如何在进度条全屏表单界面上添加百分比状态
- 《锋利的jQuery》读书笔记 第5章 jQuery对表单、表格的操作及更