c#
  • javascript
  • jquery
  • .net-3.5
  • asmx
  • 2011-08-04 559 views 0 likes 
    0

    更新:为什么我得到NaN不是Int32错误的有效值?

    我得到相同的错误。当我点击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; 
    } 
    

    任何人都知道为什么我收到那个错误?

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

    +0

    您没有包含任何关于如何调用CloseNotification的细节。 – alnorth29

    +0

    它基本上是从'x'的id中提取数字并发送给服务器。请参阅原始问题中的更新。 – nami

    回答

    2

    您有读取

    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>") 
    
    +0

    就是这样......谢谢! – nami

    0

    您在三元运算符</a>后有一个单引号。

    相关问题