获取文本框的值对Ajax ActionLink的MVC中的4 /剃刀 [英] Get textbox value for Ajax ActionLink in MVC 4 / Razor

查看:323
本文介绍了获取文本框的值对Ajax ActionLink的MVC中的4 /剃刀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做的一个按钮,一个典型的搜索字段。我使用Ajax.ActionLink的按钮,我无法弄清楚如何获得张贴在ActionLink的文本框中的值。我是这样的:

I am doing a classic search field with a button. I am using Ajax.ActionLink for the button, and I cannot figure out how to get the textbox value posted in the ActionLink. I looks like this:

<div class="input-append">
    <input type="text" id="Company_CompanyName" />
    @Ajax.ActionLink("search",
                     "CompanySearch",
                      new { searchString = "???" },
                            new AjaxOptions
                            {
                                HttpMethod = "GET",
                                InsertionMode = InsertionMode.Replace,
                                UpdateTargetId = "CompanySearchResults"
                            },
                            htmlAttributes: new { @class = "btn" })
</div>

<div id="CompanySearchResults"></div>

在哪里?是,是,我不知道如何从文本框中的值。我该怎么办?

Where the ??? is, is where I don't know how to get the value from the textbox in. What do I do?

更新1:这是一个嵌套的表格

正如我在原来的职位/的问题也提到,这是一个嵌套形式,即有提交的外在形式。因此,如果我做一个Ajax.BeginForm()里面它提交,它会调用提交的外部形态。我显然希望避免这种情况。

As I should have mentioned in the original post/question, this is a nested form, i.e. there is an outer form to be submitted. Therefore if I make a Ajax.BeginForm() with a submit inside it, it will invoke the submission of the outer form. I obviously want to avoid that.

推荐答案

好了,所以,我想尝试使其成为一个哑巴链接/按钮,并处理所有​​由JavaScript的方式传递信息。它看起来像下面这样。

Okay, so, I'd try making it a 'dumb' link/button and handling all the information passing by way of javascript. It would look something like the following.

我相信有在下面的脚本语法错误所以用它作为指导。

I believe there are syntax errors in the following script so only use it as a guideline.

<input type="text" id="Company_CompanyName" />
<a href="#" id ="CompanySearch" class="btn">Search</a>
<div id="CompanySearchResults"></div>


<script type="text/javascript">
$('#CompanySearch').click(function() {
    var searchTerm = $("#Company_CompanyName").text();
    $.ajax({
        url: @Url.Action("search","CompanySearch") ,
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        data: {'SearchTerm' : searchTerm}  // JSON.stringify(searchTerm),
        success: function(result) {
            $('#CompanySearchResults').html(result);
        }
      });
     return false;   
});
</script>

这篇关于获取文本框的值对Ajax ActionLink的MVC中的4 /剃刀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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