我的页面很简单。它显示了图片并具有注释部分(部分视图)。我只希望每当提交新评论时刷新局部视图。所以我有这个:
<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>
我唯一的问题是我不知道如何将NewComment TextBox中输入的文本传递给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>
这将生成带有名称属性值设置为“ NewCommentText”的输入元素的表单标签。
假设您的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");
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句