Las*_*org 5 parameters asp.net-ajax actionlink asp.net-mvc-2
如何将TextBox的值作为ActionLink的参数发送?
我需要使用Html.TextBoxFor
<%= Html.TextBoxFor(m => m.SomeField)%>
<%= Ajax.ActionLink("Link Text", "MyAction", "MyController", new { foo = "I need here the content of the textBox, I mean the 'SomeField' value"}, new AjaxOptions{ UpdateTargetId = "updateTargetId"} )%>
Run Code Online (Sandbox Code Playgroud)
Contoller/Actions看起来像这样:
public class MyController{
public ActionResult MyAction(string foo)
{
/* return your content */
}
}
Run Code Online (Sandbox Code Playgroud)
使用MVC 2.0
如何将TextBox的值作为ActionLink的参数发送?
将输入字段值(例如文本框)发送到服务器的语义正确方法是使用html <form>而不是链接:
<% using (Ajax.BeginForm("MyAction", "MyController", new AjaxOptions { UpdateTargetId = "updateTargetId" })) { %>
<%= Html.TextBoxFor(m => m.SomeField) %>
<input type="submit" value="Link Text" />
<% } %>
Run Code Online (Sandbox Code Playgroud)
现在,在您的控制器操作中,您将自动获取SomeField用户输入的输入值:
public class MyController: Controller
{
public ActionResult MyAction(string someField)
{
/* return your content */
}
}
Run Code Online (Sandbox Code Playgroud)
你当然可以试图违反标记语义和HTML应该通过坚持使用ActionLink即使它是错误的方式工作.在这种情况下,这是你可以做的:
<%= Html.TextBoxFor(m => m.SomeField) %>
<%= Html.ActionLink("Link Text", "MyAction", "MyController", null, new { id = "myLink" }) %>
Run Code Online (Sandbox Code Playgroud)
然后在一个单独的javascript文件中使用jQuery不引人注意地使用AJAXify这个链接:
$(function() {
$('#myLink').click(function() {
var value = $('#SomeField').val();
$('#updateTargetId').load(this.href, { someField: value });
return false;
});
});
Run Code Online (Sandbox Code Playgroud)