假设我想使用 ajax 操作链接来保存一个字段而不更改视图。
这是我的视图(它是绑定到模型类型的强类型视图):
<%= Html.TextBox("myComment", Model.MyComment)%>
<%= Ajax.ActionLink("SAVE", "SaveComment", "Home",
Model, new AjaxOptions { HttpMethod = "POST"})
%>
这是我在控制器中的 ajax actionlink 的操作:
[AcceptVerbs(HttpVerbs.Post)]
public void SaveComment(Model model)
{
MyRepository repository = new MyRepository();
Model mod = repository.GetModel(model.ID);
mod.MyComment = model.MyComment;
try
{
repository.Save();
}
catch(Exception ex)
{
throw ex;
}
}
问题是:我无法在操作方法中获取 Mycomment 文本框中的用户输入。无论用户在浏览器中为 Mycomment 输入什么,当单击 Ajax ActionLink SAVE 时,然后我检查 Mycomment 的参数模型中的值,MyComment 没有任何更改。
令人困惑的是:模型应该绑定到双向查看。但对于本例来说,似乎并非如此。
然后我将 Ajax.ActionLink 调用更改为:
Ajax.ActionLink("SAVE", "SaveComment", "Home",
new {commentinput =Model.MyComment, new AjaxOptions { HttpMethod = "POST"})
然后将操作方法签名更改为:
public void SaveComment(string commentinput)
我仍然无法获取用户输入。
如何在控制器操作方法中获取用户输入?