c# - 为什么我收到 NaN is not a valid value of Int32 错误?

标签 c# javascript jquery .net-3.5 asmx

更新:

我遇到了同样的错误。当我点击 x ,它应该返回 {id: 23},但它返回 {id: NaN}反而。如果我删除三元运算符,这个问题就会自行纠正。

<小时/>

我对我的网页进行了更改:

是这样的:

$( "#notifications" ).prepend( "<div class='notification' id='n" + notifications.d[i].id + "'><span class='notification_text'>" + notifications.d[i].text + "</span><a href='#' class='notification_button' id='b" + notifications.d[i].id + "' value='x'>x</a></div>" ).show();

我将其更改为这样,根据notifications.d[i].sticky的值添加了三元条件:

$( "#notifications" ).prepend( "<div class='notification' id='n" + notifications.d[i].id + "'><span class='notification_text'>" + notifications.d[i].text + "</span>" + ( notifications.d[i].sticky ? "" : "<a href='#' class='notification_button' id='b'" + notifications.d[i].id + "' value='x'>x</a>'" ) + "</div>" ).show();

该部分工作正常,它不会创建 x如果粘性为真,则链接。

但是,当我单击任何其他 x 时,我收到服务器端错误消息:

NaN is not a valid value of Int32

服务器端代码如下所示:

[WebMethod()]
public int CloseNotification(int id) {
    using (connection = new SqlConnection(ConfigurationManager.AppSettings["connString"])) {
        using (command = new SqlCommand("update notifications set closed_by = @user where id = @id", connection)) {
            command.Parameters.Add("@id", SqlDbType.Int, 4).Value = id;
            command.Parameters.Add("@user", SqlDbType.VarChar, 4).Value = "abc";

            connection.Open();
            intAffectedRows = command.ExecuteNonQuery();
            connection.Close();
        }
    }

    return intAffectedRows;
}

有人知道我为什么会收到这个错误吗?

我想我在三元部分犯了一个错误,可能是双引号或单引号错误,但我看不到它。

最佳答案

您的代码行中存在语法错误

id='b'" + notifications.d[i].id + "' value='x'>x</a>'" ) + "</div>" ).show();

b之后有一个额外的'。它应该是:

id='b" + notifications.d[i].id + "' value='x'>x</a>'" ) + "</div>" ).show();

编辑:

在三元运算的末尾还有一个额外的 '

( notifications.d[i].sticky ? "" : "<a href='#' class='notification_button' id='b" + notifications.d[i].id + "' value='x'>x</a>'" )

应该是:

( notifications.d[i].sticky ? "" : "<a href='#' class='notification_button' id='b" + notifications.d[i].id + "' value='x'>x</a>" )

关于c# - 为什么我收到 NaN is not a valid value of Int32 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6944184/

相关文章:

javascript - Anime.js 与 Wordpress : module is not defined

javascript - jQuery,如果任何 "one"值发生变化

jquery - 有没有 JQuery map 插件

javascript - 如何在由 .(点)或 ,(逗号)作为千位分隔符分隔的数字上使用 jquery 函数

c# - System.Threading.Tasks 中的 NullReferenceException 调用 HttpClient.GetAsync(url)

c# - 声明类型为 "emails",然后 ClaimTypes.Email 为 null

javascript - Handlebars : More than one model inside template

javascript - Wow.js 无法在服务器中运行

c# - LINQ to XML(动态 XML)

javascript - 从 C# 静态 Web 方法(页面方法)调用 Javascript 函数