加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-2 – 将参数传递给MVC Ajax.ActionLink

发布时间:2020-12-16 07:11:43 所属栏目:asp.Net 来源:网络整理
导读:如何将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' v
如何将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"} )%>

Contoller / Actions看起来像这样:

public class MyController{
   public ActionResult MyAction(string foo)
   {      
      /* return your content */   
   }
}

使用MVC 2.0

解决方法

How can I send the value of the TextBox as a parameter of the ActionLink?

将输入字段值(例如文本框)发送到服务器的语义正确方式是使用html< form>而不是链接:

<% using (Ajax.BeginForm("MyAction",new AjaxOptions { UpdateTargetId = "updateTargetId" })) { %>
    <%= Html.TextBoxFor(m => m.SomeField) %>
    <input type="submit" value="Link Text" />
<% } %>

现在,在您的控制器操作中,您将自动获取用户输入的SomeField输入的值:

public class MyController: Controller
{
    public ActionResult MyAction(string someField)
    {      
       /* return your content */   
    }
}

你当然可以尝试通过坚持使用ActionLink来违反标记语义和HTML应该工作的方式,即使它是错误的.在这种情况下,这是你可以做的:

<%= Html.TextBoxFor(m => m.SomeField) %>
<%= Html.ActionLink("Link Text",null,new { id = "myLink" }) %>

然后在一个单独的javascript文件中使用jQuery不引人注意地使用AJAXify这个链接:

$(function() {
    $('#myLink').click(function() {
        var value = $('#SomeField').val();
        $('#updateTargetId').load(this.href,{ someField: value });
        return false;
    });
});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读