在ASP.NET MVC中获取输入值或JavaScript变量Ajax.ActionLink

时间:2009-04-12 23:41:14

标签: asp.net asp.net-mvc

我想将输入控件值(例如textbox1.value或javascript变量)传递给控制器​​操作方法(作为参数),而不使用表单帖子(使用Ajax.ActionLink)。请参阅下面的代码。

是否可以在Ajax.ActionLink中分配new {name = textbox1.value}之类的内容。

查看

<input type="text" id="textbox1" />
<%= Ajax.ActionLink("mylink", "linkfunction", new {name = textbox1.value}, new AjaxOptions { UpdateTargetId = "result"}) %>
<span id="result"></span>

和控制器操作是:

public string linkfunction(string name)
{
    return  DateTime.Now.ToString();
}

3 个答案:

答案 0 :(得分:1)

这与此类似:

ASP.NET MVC : AJAX ActionLink- Target an HTML attribute

另外,您无需将控件名称传入Action。

答案 1 :(得分:0)

我有同样的问题,除了我使用jQuery来发出我的ajax请求:

$('#ajax-content').load('<%= this.Url.Action("Details", "Page", new { id = someJavascriptVariable }) %>');

我让它像这样工作:

$('#ajax-content').load('<%= this.Url.Action("Details", "Page", ) %>' + '/' + someJavascriptVariable);

或者,在您的情况下,它可能如下所示:

$('#ajax-content').load('<%= this.Url.Action("Details", "Page", ) %>' + '?name=' + textbox1.value);

答案 2 :(得分:0)

我有类似的问题。一旦向我指出这些函数返回字符串,其余部分就会到位 - 所以,请尝试以下

Ajax.ActionLink("mylink", "linkfunction", 
    new {name = __MyValue__}, new AjaxOptions 
    { UpdateTargetId = "result"}).Replace("__MyValue__", textbox1.value)

我没有测试过这个片段,但这样的事情对我来说效果很好。

祝你好运。