我得到相同的错误。当我点击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;
}
任何人都知道为什么我收到那个错误?
我在想我已经犯了三元错误,可能是双引号或单引号错误,但我看不到它。
您没有包含任何关于如何调用CloseNotification的细节。 – alnorth29
它基本上是从'x'的id中提取数字并发送给服务器。请参阅原始问题中的更新。 – nami