2

使用 C# 和 Webmatrix 处理我的第一个网页项目。所以事情一直进展顺利,但现在遇到了一个让我难过的问题。我正在做的是在此页面上发布一个包含一条消息的表单,然后我将消息保存到数据库(稍后还会让电子邮件部分继续运行)。

我已经发布了下面的代码,它实际上比说明问题所需的时间更长,但我认为我还是会发布它。当我看到倒数第二个右大括号以黄色突出显示时,WebMatrix 已经在我尝试运行它之前突出显示了手头的问题。运行代码时(并且 post 值都可以),我收到错误“CS1513:} 预期”。我最初以为我错误地添加了一个额外的右花括号,但是在检查和仔细检查然后慢慢地逐行重建代码以识别在哪一行之后开始出错,我现在得出的结论是没有任何“if(IsPost && WebSecurity.IsAuthenticated)”“if”语句中嵌套的“if”和“foreach”语句,然后“

我检查了一下,发现很多 C# 代码示例都是这样做的。任何帮助,指针或其他任何东西都非常感谢。

@{
if(IsPost && WebSecurity.IsAuthenticated){
    var fromEmail = WebSecurity.CurrentUserName;
    var fromUserId = @WebSecurity.CurrentUserId;
    var message = Request["message"];
    var uid = Request["uid"];
    bool multiSend = false;
    var db = Database.Open("Tennegize");

    var skillLevel = 0;
    if(Request["multi"]=="1"){
        multiSend = true;
    }

    //Save to message to BD
    var insertQuery = "INSERT INTO Messages (UserEmail, UserId, Message) VALUES (@0, @1, @2)";
    db.Execute(insertQuery, fromEmail, fromUserId, message);
    decimal messageId = db.GetLastInsertId();

    if(multiSend){
         //Get email address' from all recipients
        var sql2 = "SELECT Email FROM UserProfile WHERE SkillLevel = @0";
        var data2 = db.Query(sql, skillLevel);
        var totalRecipents = data2.Count();
        //Log all recipients who will recieve this notification
        foreach(var recipient in data2){
            insertQuery = "INSERT INTO Message_Recipients (UserId, UserEmail, MessageId) VALUES (@0, @1, @2)";
            db.Execute(insertQuery, uid, data.Email, messageId); 
        }

    }else{
        // Get to recipients email address
        var sql = "SELECT Email FROM UserProfile WHERE UserId = @0";
        var data = db.QuerySingle(sql, uid);
        //Log the recipient of this norification
        insertQuery = "INSERT INTO Message_Recipients (UserId, UserEmail, MessageId) VALUES (@0, @1, @2)";
        db.Execute(insertQuery, uid, data.Email, messageId);

    }

    }
}
4

1 回答 1

4

我的猜测是这条线var fromUserId = @WebSecurity.CurrentUserId;导致了问题。删除@之前的WebSecurity.CurrentUserId.

于 2012-11-05T14:31:55.730 回答