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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么