【问题标题】:MVC Ajax: How to send string from view to controllerMVC Ajax:如何将字符串从视图发送到控制器
【发布时间】:2020-03-21 21:43:37
【问题描述】:

与发送 json 对象相比,我发现通过 ajax 发送纯文本(字符串)有一个小问题。

我目前正在使用此设置

(cs)html

<label for="search">
<i class="fa fa-search" onclick="sendtoC()"></i>
<input type="search" id="search" placeholder="Sök här..." autofocus; />
<input type="button" id="SÖK" value="SÖK" onclick="sendtoC()" />

脚本

<script>
var invalue;
var input = document.getElementById("search");
input.addEventListener("keyup", function go (event) {
    if (event.keyCode === 13) {
        invalue = document.getElementById("search").value;
        sendtoC(invalue);
    }
});

function sendtoC() {
    $.ajax({
    url: "/Home/SearchResults",
    dataType: "json",
    type: "GET", 
    contentType: 'application/json; charset=utf-8', //define a contentType of your request
    cache: false,
    data: { input: invalue },
        success: function (data) {
        if (data.success) {
            alert(data.message);
        }
    },
    error: function (xhr) {
        alert('error');
    }
});
}

和电流控制器

public ActionResult SearchResults(string input)
    {
        Data.gsPersonLista = db.GetPerson(input);
        return Json(new { success = true, message = input }, JsonRequestBehavior.AllowGet);


    }

我想直接向控制器发送一个字符串,我尝试了这个脚本

function sendtoC() {
$.ajax({
url: "/Home/SearchResults",
dataType: "text",
type: "GET", 
contentType: 'application/json; charset=utf-8', //define a contentType of your request
cache: false,
data: invalue ,
    success: function (data) {
    if (data.success) {
        alert(data.message);
    }
},
error: function (xhr) {
    alert('error');
}});}

使用这个控制器

 public ActionResult SearchResults(string input)
        {
            Data.gsPersonLista = db.GetPerson(input);
            return View(input);


        }

但是这不起作用,输入字符串显示为 null 并且 ajax 给出了错误。我目前不知道如何解决这个问题,也不知道是什么导致了错误。如果有人能指出我正确的方向,我将不胜感激

【问题讨论】:

  • 该字符串应该在 sql 查询中使用,但我将在实际获取字符串后处理该问题。

标签: c# json ajax asp.net-mvc model-view-controller


【解决方案1】:

您可以在这里简单地使用$.get 函数,其次您应该使用Ùrl.Action 帮助器来获取针对控制器操作方法的url,因为魔术字符串会在部署中导致应用程序可能部署在子目录中的问题- 目录,在这种情况下,网址会出错:

$.get('@Url.Action("SearchResults","Home")?input='+invalue , function (data) {  

    if (data.success) {
         alert(data.message);
    }
});  

【讨论】:

  • 喂!这有点工作,虽然调试“invalue”在移动到新视图时从输入中获取正确的值,但是没有来自成功的警报,这很奇怪。
  • 尝试输入:alert(data) 并查看或console.log(data) 并检查浏览器控制台
【解决方案2】:

您可以轻松地将其作为请求参数传递,因为您还将类型设置为“GET”。

url: "/Home/SearchResults?input="+invalue

您还必须删除数据属性。如果有帮助,请告诉我。

【讨论】:

    【解决方案3】:

    更新答案 数据类型是您期望从服务器返回的。内容类型是您发送的内容。因此将视图更改数据类型返回为 htmL。

    dataType: 'html',
    

    问题在于,当您调用函数 sendtoC 时,您没有收到函数中的任何参数。更改函数以接受参数。

    var invalue;
     var input = document.getElementById("search");
     input.addEventListener("keyup", function go (event) {
      if (event.keyCode === 13) {
        invalue = document.getElementById("search").value;
        sendtoC(invalue);
    }
      });
    
    function sendtoC(invalue ) {
    $.ajax({
    url: "/Home/SearchResults",
    dataType: "json",
    type: "GET", 
    contentType: 'application/json; charset=utf-8', //define a contentType of your request
    cache: false,
    data: { input: invalue },
        success: function (data) {
        if (data.success) {
            alert(data.message);
        }
    },
    error: function (xhr) {
        alert('error');
    }
    });
    }
    

    【讨论】:

    • 这个例子是已经可以工作的代码,它下面的代码(数据类型:文本)是真正的“问题”。添加参数并没有改变任何东西。我将尝试将其添加到有问题的代码中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-08
    • 1970-01-01
    • 1970-01-01
    • 2011-08-09
    • 2021-12-20
    • 2021-04-09
    • 2015-09-01
    相关资源
    最近更新 更多