weixin_33725515 2016-02-03 14:37 采纳率: 0%
浏览 27

控制器未接收数据?

i'm trying to send data from a view to a controller using POST but i doesn't seems to work.

Here's my code (javascript function) :

function showHint(str) {
if (str.length == 0) {
    document.getElementById("txtHint").innerHTML = "";
    return;
} else {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
        }
    };
    var titre = str;
    xmlhttp.open("POST", "CheckReturn", true);
    //xmlhttp.send(titre);
    xmlhttp.send({"titre":titre});
}

}

the html triggering it :

    <div id="check-name" class="form-row">
    <input id="name" type="text" name="name" placeholder="Titre de l'événement" onkeyup="showHint(this.value)">
    <span id="txtHint"></span>
</div>

and finally, the controller SUPPOSED to get it :

        [HttpPost]
    public ActionResult CheckReturn(string titre)
    {
        var contexte = new intranetEntities();
        var ajax = contexte.evenementiel.SqlQuery("Somequery" + titre);
        ViewBag.ajax = ajax;

        return PartialView();
    }

i checked bit firebug, the data is sent, but with visual studio's debugger, i see that "titre" always stay null.

What can i do ?

  • 写回答

1条回答 默认 最新

  • weixin_33682719 2016-02-03 15:10
    关注

    I've run into something similar in the past with asp.net but I never really tried to understand what's happening. What fixed it for me was the following:

    make a new class somewhere in your back-end (anywhere, doesn't matter for now)

    class TitreWrapper {
     public string titre {get; set;}
    }
    

    then in your controller

    [HttpPost]
    public ActionResult CheckReturn(TitreWrapper titreWrap)
    {
      string titre = titreWrap.titre;
    }
    

    this should work according to my previous experience


    the top answer is wrong (but right in other circumstances I think)

    for this problem the following worked:

    xmlhttp.open("POST", "/CheckReturn?titre=" + titre, true);
    

    and then just

    xmlhttp.send(); 
    

    OR

    xmlhttp.open("POST", "CheckReturn", true);
    

    then

    xmlhttp.send("titre=" + titre);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)