如何在asp.net mvc 3中使用@html.actionlink传递文本框值

时间:2022-12-02 11:38:04

How to Pass value from text using @html.actionlink in asp.net mvc3 ?

如何在asp.net mvc3中使用@html.actionlink传递文本中的值?

4 个解决方案

#1


5  

Rather than passing your value using @Html.actionlink, try jquery to pass your textbox value to the controller as:

不要使用@ Html.actionlink传递您的值,而是尝试使用jquery将您的文本框值传递给控制器​​:

$(function () {
    $('form').submit(function () {
        $.ajax({
            url: this.action,
            type: this.method,
            data: { search: $('#textboxid').val()},
            success: function (result) {
                $('#mydiv').html(result);
            }
        });
        return false;
    });
});

This code will post your textbox value to the controller and returns the output result which will be loaded in the div "mydiv".

此代码将您的文本框值发布到控制器并返回将在div“mydiv”中加载的输出结果。

#2


11  

None of the answers here really work. The accepted answer doesn't refresh the page as a normal action link would; the rest simply don't work at all or want you to abandon your question as stated and quit using ActionLink.

这里的答案都没有真正奏效。接受的答案不会刷新页面,因为正常的操作链接会;其余的根本不起作用或者希望你放弃你所说的问题并使用ActionLink退出。

MVC3/4

MVC3 / 4

You can use the htmlAttributes of the ActionLink method to do what you want:

您可以使用ActionLink方法的htmlAttributes来执行您想要的操作:

Html.ActionLink("My Link Title", "MyAction", "MyController", null, new { onclick = "this.href += '&myRouteValueName=' + document.getElementById('myHtmlInputElementId').value;" })

MVC5

MVC5

The following error has been reported, but I have not verified it:

已报告以下错误,但我尚未验证:

A potentially dangerous Request.Path value was detected

检测到潜在危险的Request.Path值

#3


2  

to pass data from the client to the server you could use a html form:

要将数据从客户端传递到服务器,您可以使用html表单:

  @using (Html.BeginForm(actionName,controllerName)) {
    <input type="text" name="myText"/>
    <input type="submit" value="Check your value!">
}

be sure to catch your myText variable inside your controller's method

一定要在控制器的方法中捕获myText变量

#4


-3  

you can use this code (YourValue = TextBox.Text)

你可以使用这段代码(YourValue = TextBox.Text)

Html.ActionLink("Test", new { controller = "YourController", action = "YourAction", new { id = YourValue }, null );

public class YourController : Controller
{
        public ActionResult YourAction(int id)
        {
            return View("here your value", id);
        }
}

#1


5  

Rather than passing your value using @Html.actionlink, try jquery to pass your textbox value to the controller as:

不要使用@ Html.actionlink传递您的值,而是尝试使用jquery将您的文本框值传递给控制器​​:

$(function () {
    $('form').submit(function () {
        $.ajax({
            url: this.action,
            type: this.method,
            data: { search: $('#textboxid').val()},
            success: function (result) {
                $('#mydiv').html(result);
            }
        });
        return false;
    });
});

This code will post your textbox value to the controller and returns the output result which will be loaded in the div "mydiv".

此代码将您的文本框值发布到控制器并返回将在div“mydiv”中加载的输出结果。

#2


11  

None of the answers here really work. The accepted answer doesn't refresh the page as a normal action link would; the rest simply don't work at all or want you to abandon your question as stated and quit using ActionLink.

这里的答案都没有真正奏效。接受的答案不会刷新页面,因为正常的操作链接会;其余的根本不起作用或者希望你放弃你所说的问题并使用ActionLink退出。

MVC3/4

MVC3 / 4

You can use the htmlAttributes of the ActionLink method to do what you want:

您可以使用ActionLink方法的htmlAttributes来执行您想要的操作:

Html.ActionLink("My Link Title", "MyAction", "MyController", null, new { onclick = "this.href += '&myRouteValueName=' + document.getElementById('myHtmlInputElementId').value;" })

MVC5

MVC5

The following error has been reported, but I have not verified it:

已报告以下错误,但我尚未验证:

A potentially dangerous Request.Path value was detected

检测到潜在危险的Request.Path值

#3


2  

to pass data from the client to the server you could use a html form:

要将数据从客户端传递到服务器,您可以使用html表单:

  @using (Html.BeginForm(actionName,controllerName)) {
    <input type="text" name="myText"/>
    <input type="submit" value="Check your value!">
}

be sure to catch your myText variable inside your controller's method

一定要在控制器的方法中捕获myText变量

#4


-3  

you can use this code (YourValue = TextBox.Text)

你可以使用这段代码(YourValue = TextBox.Text)

Html.ActionLink("Test", new { controller = "YourController", action = "YourAction", new { id = YourValue }, null );

public class YourController : Controller
{
        public ActionResult YourAction(int id)
        {
            return View("here your value", id);
        }
}