将参数传递给MVC Ajax.ActionLink [英] Passing parameters to MVC Ajax.ActionLink

查看:159
本文介绍了将参数传递给MVC Ajax.ActionLink的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何发送文本框的值作为ActionLink的的参数?

我需要使用Html.TextBoxFor

 <%= Html.TextBoxFor(M = GT; m.SomeField)%GT;
<%= Ajax.ActionLink(链接文本,MyAction,myController的新富{=我在这里所需要的文本框的内容,我的意思是'SomeField价值},新AjaxOptions {UpdateTargetId = updateTargetId})%GT;

该位指示/操作如下:

 公共类{myController的
   公众的ActionResult MyAction(串富)
   {
      / *返回你的内容* /
   }
}

使用MVC 2.0


解决方案

  

我如何发送文本框的值作为ActionLink的的参数?


发送输入的语义正确的方式字段的值(如文本框)到服务器是通过使用HTML <形式> ,而不是链接:

 <%使用(Ajax.BeginForm(MyAction,myController的新AjaxOptions {UpdateTargetId =updateTargetId})){%GT;
    &所述;%= Html.TextBoxFor(M => m.SomeField)%GT;
    <输入类型=提交值=链接文本/>
<%}%GT;

现在在你的控制器动作,你将自动获得由用户输入的 SomeField 输入的值:

 公共类myController的:控制器
{
    公众的ActionResult MyAction(字符串someField)
    {
       / *返回你的内容* /
    }
}

您当然可以尝试违反语义标记和HTML的方式应该是通过坚持使用工作的 ActionLink的哪怕是错的。在这种情况下,这里是你能做什么:

 <%= Html.TextBoxFor(M = GT; m.SomeField)%GT;
<%= Html.ActionLink(链接文本,MyAction,myController的,空,新的{ID =myLink的})%GT;

,然后在一个单独的JavaScript文件悄悄地AJAXify此链接使用jQuery:

  $(函数(){
    $('#myLink的')。点击(函数(){
        。VAR值= $('#SomeField)V​​AL();
        $('#updateTargetId')负载(this.href,{someField:值})。
        返回false;
    });
});

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

I need to use the 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"} )%>

The Contoller/Actions looks like this:

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

Using MVC 2.0

解决方案

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

The semantically correct way of sending input fields values (such as textboxes) to a server is by using an html <form> and not links:

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

Now in your controller action you will automatically get the value of the SomeField input entered by the user:

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

You could of course try to violate the markup semantics and the way HTML is supposed to work by insisting on using an ActionLink even if it is wrong. In this case here's what you could do:

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

and then in a separate javascript file unobtrusively AJAXify this link using jQuery:

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

这篇关于将参数传递给MVC Ajax.ActionLink的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆