Getting the value of an input or JavaScript variable in ASP.NET MVC Ajax.ActionLink

I want to pass an input control value (e.g., textbox1.valueor javascript variable) to a controller action method (as a parameter) without a form message (using Ajax.ActionLink). See code below.

Is it possible to assign something like new {name = textbox1.value}in Ajax.ActionLink.

View

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

and controller action:

public string linkfunction(string name)
{
    return  DateTime.Now.ToString();
}
+3
source share
3 answers

It looks like this:

ASP.NET MVC: AJAX ActionLink - HTML Target Attribute

In addition, you do not need to pass the name of the control into your action.

+1
source

I had the same problem, except that I used jQuery for my ajax request:

$('#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);
0

I had a similar problem. As soon as I was told that these functions return strings, the rest will remain in place - so try the following

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

I have not tested this snippet, but something like this worked very well for me.

Good luck.

0
source

Source: https://habr.com/ru/post/1706306/


All Articles