简体   繁体   English

里面的代码<script> tag not registering

[英]code inside a <script> tag not registering

I'm trying to send an AJAX post request from my View to Controller to add new data into my database.我正在尝试从我的视图向控制器发送一个 AJAX 发布请求,以将新数据添加到我的数据库中。 But when I write a script to do so inside my View (Index.cshtml), it wouldn't work.但是当我在我的视图 (Index.cshtml) 中编写一个脚本来执行此操作时,它不起作用。 I tried setting a breakpoint inside the function, and it says我尝试在函数内部设置断点,它说

The breakpoint will not currently be hit.当前不会命中断点。 No executable code of the debugger's target code type is associated with this line.没有调试器目标代码类型的可执行代码与此行相关联。 Possible causes include: conditional compilation, compiler optimizations, or the target architecture of this line is not supported by the current debugger code type.可能的原因包括:条件编译、编译器优化或当前调试器代码类型不支持该行的目标架构。

Disclaimer: it is possible that I'm doing this completely wrong, but I think I'm not, and I'm very confused why I'm getting this error.免责声明:我这样做可能完全错误,但我认为我不是,而且我很困惑为什么我会收到这个错误。 How can I do this properly?我怎样才能正确地做到这一点?

The script I'm trying to run:我试图运行的脚本:

<script>
    $("#AddFriend").click(function () {
        mydata = { AddName: $("#AddName").val(), AddLastName: $("AddLastName").val(), AddAge: $("AddAge").val(), AddTag: $("AddTag").val() }
        $.ajax({
            url: "/Home/AddFriend",
            type: 'Post',
            data: mydata,
            success: null
        });
    });
</script>

The section of the code it refers to:它引用的代码部分:

             <div>
                <label>Name:</label><input id="AddName" type="text" /> <br />
                <label>Last name:</label><input id="AddLastName" type="text" /> <br />
                <label>Age:</label><input id="AddAge" type="text" /> <br />
                <label>Tag:</label><input id="AddTag" type="text" /> <br />
                <label id="AddLabel"></label>
                <button id="AddFriend">Submit</button>
            </div>

The function inside the Controller the AJAX code should reach: AJAX 代码应该达到的控制器内部的功能:

    [HttpPost]
    public void AddFriend(string AddName, string AddLastName, string AddAge, string AddTag)
    {
        using (var context = new ContactEntities1())
        {
            Friend fr = new Friend();
            fr.userId = 1;
            fr.Name = AddName;
            fr.LastName = AddLastName;
            fr.Age = Convert.ToInt32(AddAge);
            fr.Tag = AddTag;
            context.Friend.Add(fr);
            context.SaveChanges();
        }
    }

(for the record, I'm setting the breakpoint at the following line:) (为了记录,我在以下行设置断点:)

mydata = { AddName: $("#AddName").val(), AddLastName: $("AddLastName").val(), AddAge: $("AddAge").val(), AddTag: $("AddTag").val() }

to answer the question回答问题

the click is not firing at all, I tried adding a line that changed a value of a label at the start of the click function, and it wouldn't execute either, so I think the click function itself is not executing点击根本没有触发,我尝试在点击功能开始时添加一行更改标签值的行,但它也不会执行,所以我认为点击功能本身没有执行

I think your just missing a ready() :我认为你只是缺少一个ready()

 $(document).ready(function() { $("#AddFriend").click(function () { alert('click fired'); }); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div> <label>Name:</label><input id="AddName" type="text" /> <br /> <label>Last name:</label><input id="AddLastName" type="text" /> <br /> <label>Age:</label><input id="AddAge" type="text" /> <br /> <label>Tag:</label><input id="AddTag" type="text" /> <br /> <label id="AddLabel"></label> <button id="AddFriend">Submit</button> </div>

The .ready() method offers a way to run JavaScript code as soon as the page's Document Object Model (DOM) becomes safe to manipulate.一旦页面的文档对象模型 (DOM) 变得可以安全操作, .ready()方法就提供了一种运行 JavaScript 代码的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM