C# Javascript/Ajax中的ViewBag数据未正确传入

C# Javascript/Ajax中的ViewBag数据未正确传入,c#,javascript,ajax,asp.net-mvc,C#,Javascript,Ajax,Asp.net Mvc,我有以下脚本 <script> $("#sendMSG").click(function () { $.ajax({ type: "POST", url: '@Url.Action("Action", "Controller")', dataType: "JSon", data: { "Email": '@ViewBag.Email' }, success: function (data) {

我有以下脚本

<script>
$("#sendMSG").click(function () {
    $.ajax({
        type: "POST",
        url: '@Url.Action("Action", "Controller")',
        dataType: "JSon",
        data: { "Email": '@ViewBag.Email' },

        success: function (data) { 
             document.getElementById("Output").innerHTML = data.Message;},
        error: console.log("It did not work"),
    });
});
</script>
它返回一个空值。如果您对这可能不起作用的原因有任何想法,或者需要更多的解释/代码,请让我知道,我很乐意提供

谢谢

编辑:

提交我的其他信息的原始表单代码

 [[Form_SmsPhone]]
  <form action="\Controller\Action" id="smsPhone" method="post">
  [[/Form_SmsPhone]]
    [[Block_Sms]]
    <div class="input-append">
        <input id="phonenumber" style="width: 115px;" name="phonenumber"       type="text">
        <button class="btn" type="submit" value="Send"   id="sendMsg">Send</button>
    </div>
    <p id ="Output"></p>
    [[/Block_Sms]]
  [[Form_End]]
    </form>
  [[/Form_End]]
[[Form_SmsPhone]]
[Form_SmsPhone]]
[[Block_Sms]]
发送

[/Block_Sms]] [[表格结束]] [/Form_End]]
您的按钮类型为
submit

<button class="btn" type="submit" value="Send"   id="sendMsg">Send</button>
这将阻止表单提交

编辑:


此外,您的按钮名为“sendMsg”,但您将单击事件挂接到“sendMsg”。那是行不通的。案子很重要。

不要把
@ViewBag.Email'
,它只是
@ViewBag.Email
@AnnArbor87:错了。这将创建一个Javascript语法错误您如何调用操作
谢谢
?jQuery请求似乎正在调用某个控制器的某个操作
action
。如果没有“”,它将被呈现为数据:{“Email”:someemail@email.com }这不是有效的json,因此需要“”号。加载后,您能在页面的源代码中正确显示电子邮件地址吗?不幸的是,我仍然无法获取电子邮件:(还有其他想法吗?在您的评论和代码中,您对操作和控制器名称非常挑剔(例如
/Controller/Action
@Url.Action(“Action”,“Controller”)
).但是,在上面的代码中,您将您的操作命名为
Thankyu
。您的Ajax是调用此
Thankyu
操作还是其他什么?哎呀。是的,我肯定lol我已经正确地调用了它们。查看我的编辑,我认为这是您的问题的根源。您还需要
返回false
来防止正常提交。您的Ajax是ne非常爱你。谢谢你:D…nvm解决了。再次感谢!
<button class="btn" type="submit" value="Send"   id="sendMsg">Send</button>
$("#sendMSG").click(function () {
    $.ajax({
        ...
    });
    return false;
});