ASP.NET Web Pages C#代码块中无法匹配的大括号

时间:2013-09-19 15:23:46

标签: c# asp.net if-statement razor-2

“连接到MPP并寻找现有驱动程序ID”下的If Else块在我的主Razor块中引起无法匹配的花括号。我知道我错过了什么,但无法弄清楚。 调试器忽略了最后一个花括号,看到最后一个花括号的下一个与@ {开口大括号匹配。

@{
Layout = "~/Shared/_Layout.cshtml";
Page.Title = "Register";

// Setup field validation
Validation.RequireField("driverfirstname", "Cannot be blank.");
Validation.RequireField("driverlastname", "Cannot be blank.");
Validation.RequireField("driverssn", "Cannot be blank.");

// Initialize variables
var driverfirstname = "";
var driverlastname = "";
var driverssn = "";

// If validation is OK    
if (IsPost && Validation.IsValid())
{
    driverfirstname = @Request.Form["driverfirstname"];
    driverlastname = @Request.Form["driverlastname"];
    driverssn = @Request.Form["driverssn"];

    // Create Driver ID and Password
    string first = driverfirstname.Substring(0, 3);
    string last = driverlastname.Substring(0, 3);
    string lastFour = driverssn.Substring(7, 4);
    string drivercodetest = (last + first).ToUpper();

    // Connect to MPP and look for existing Driver IDs
    var dbs = Database.Open("other_db");
    var drivercodempp = dbs.Query("SELECT mpp_id FROM profile WHERE mpp_id = @drivercodetest");
    var drivercode = "";

    if (drivercodempp == null)
    {
        drivercode = (last + first).ToUpper();
    }
    else if (drivercodempp.Contains("01"))
    {
        drivercode = (last + first).ToUpper() + "02";
    }
    else if (drivercodempp.Contains("02"))
    {
        drivercode = (last + first).ToUpper() + "03";
    }
    else if (drivercodempp.Contains("03"))
    {
        drivercode = (last + first).ToUpper() + "04";
    }
    else if (drivercodempp.Contains("04"))
    {
        drivercode = (last + first).ToUpper() + "05";
    }
    else if (drivercodempp.Contains("05"))
    {
        drivercode = (last + first).ToUpper() + "06";
    }
    else
    {
        drivercode = (last + first).ToUpper() + "07";
    }

    // Submit to database
    var dbi = Database.Open("MainDB");
    var insertCommand = "INSERT INTO driver (drivercode, driverfirstname, driverlastname, driverssn) VALUES(@0, @1, @2, @3)";
    dbi.Execute(insertCommand, drivercode, driverfirstname, driverlastname, driverssn);

    Response.Redirect("~/confirm_register");
}

}

2 个答案:

答案 0 :(得分:0)

您的if (IsPost && Validation.IsValid())行缺少其右大括号。

答案 1 :(得分:0)

我也遇到了这个问题。 我认为您的问题与您的@Request变量有关。 尝试摆脱变量名前面的@符号,它应该为您解决问题。当你在@ {}块时(你可以直接访问Request),你不需要@符号,显然它实际上会导致花括号不匹配。我的猜测是@导致Razor的解析器混淆并提前退出代码块。其他语法违规也会导致它,所以戴的帖子也可能适用。

// If validation is OK    
if (IsPost && Validation.IsValid())
{
    driverfirstname = Request.Form["driverfirstname"];
    driverlastname = Request.Form["driverlastname"];
    driverssn = Request.Form["driverssn"];

您还可能需要解决几个@variable语法的用法:

var drivercodempp = dbs.Query("SELECT mpp_id FROM profile WHERE mpp_id = @drivercodetest");

var insertCommand = "INSERT INTO driver (drivercode, driverfirstname, driverlastname, driverssn) VALUES(@0, @1, @2, @3)";

最后一个你可能需要用@@编码@,因为你实际上需要@ literal。 如果我没有捕获所有语法违规,那么就开始删除大部分代码并一次将它们重新粘贴,直到找到令Razzor解析器混淆的内容。

第二个问题是,大部分逻辑可能会被移出您的视图,移动到您的控制器或存储库中,以更好地遵循关注点分离的MVC模式。但我认为这里的根本原因是语法问题导致您提前退出Razor代码块。