1

“连接到 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");
}

}

4

2 回答 2

0

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

// 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)";

最后一个你可能需要用@@ 编码@,因为你实际上需要@ 文字。如果我没有发现所有的语法违规,那么开始剪切大部分代码并将它们一次粘贴回一行,直到你找到使 Razor 解析器感到困惑的地方。

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

于 2013-10-30T18:42:52.623 回答
0

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

于 2013-09-19T16:53:07.270 回答