【问题标题】:Grab TextBox-Value in cshtml from JavaScript从 JavaScript 中获取 cshtml 中的 TextBox-Value
【发布时间】:2014-09-29 07:11:28
【问题描述】:

我想用 JavaScript 调用 ActionUrl。我的视图中有几个文本框。像这样:

查看:

@Html.Label("Suchbegriff")
@Html.TextBox("GeneralSearchTextBox")

@Html.Label("Strasse")
@Html.TextBox("StreetTextBox")

我有一个这样的控制器:

控制器:

public ActionResult FindAdress(int id = 0, string street = "", string city = "", string plz = "", string name = "")
    {
        DoSomethingSpecialWithTheParameters;
    }

我的问题是,我想使用 JavaScript 将 TextBoxes 中的值传递到我的 FindAdress(...) 方法中。

我开始像这样构建一个 JavaScript-Function:

查看(只是脚本部分):

function search()
    {
        window.navigate('@Url.Action("FindAdress", "Home",
                        new
                        {
                            id=3,
                            street = ?????
                        },
                        null)');
    }

我不知道如何从这里访问文本框。 有什么想法吗?

非常感谢。

【问题讨论】:

  • 您的search函数与输入是否在同一个cshtml文件中?
  • 是的,它在同一个文件中..

标签: javascript c# asp.net-mvc-4


【解决方案1】:

假设你想重定向到FindAdress()方法

function search() {
  var street = document.getElementById("StreetTextBox").value;
  var id = ....
  var city = ....
  // other values
  var url = '@Url.Action("FindAdress", "Home")';
  document.location.href = url + '?id=' + id + '&street=' + street + '&city=' + city; // plus other values

【讨论】:

    【解决方案2】:

    $("#textBoxId").val():

    应该是获取文本框值的答案。我错过了什么吗?

    【讨论】:

      猜你喜欢
      • 2014-06-04
      • 2011-03-19
      • 2017-09-12
      • 2016-09-23
      • 1970-01-01
      • 1970-01-01
      • 2020-08-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多