asp.net-mvc-4 - Razor 看不到我的右括号

标签 asp.net-mvc-4 razor-2

我在使用 Razor2 View 时遇到了一个非常奇怪的问题。下面的代码是 实际代码,intellisense 在其上 throw 红色波浪线,只是名称已更改。

@model Space.ViewModels.PropertyAdminViewModel

@{
    ViewBag.Title = "Create";
    ViewBag.UmbracoTitle = "Create Property";
    Layout = "../Shared/_Layout.cshtml";

    InitView();
    AnalysePreviousState();
    SortErrorsPerStep();
    GetStepToDisplay();
}

<script src="../../Scripts/jquery-1.8.2.min.js"></script>
<script src="../../Scripts/jquery.validate.min.js"></script>
<script src="../../Scripts/jquery.validate.unobtrusive.min.js"></script>
<script src="../../Scripts/AdminScripts.js"></script>

@Html.ValidationSummary(ErrorMessage)

@using (Html.BeginForm())
{
    @Html.Hidden("CurrentStep", StepToDisplay.ToString(), new { id="CurrentStep"})
    <input id="AddressButton" type="submit" name="@Button.AddressButton.ToString()" value="Address Details" />
    <input id="DetailsButton" type="submit" name="@Button.DetailsButton.ToString()" value="Details &amp; Description" />
    <input id="MediaButton" type="submit" name="@Button.MediaButton.ToString()" value="Images &amp; Documents" />


    switch (StepToDisplay)
    {
        case Step.Address:
            Html.RenderPartial("_AddressDetailsForm", ViewData.Model.Address);
            break;
        case Step.Details:
            Html.RenderPartial("_PropertyDetailsForm", ViewData.Model);
            break;
        case Step.Media:
            Html.RenderPartial("_MediaUploadEditForm", ViewData.Model.MediaItems);
            break;
    }


    <input id="BackButton" type="submit" name="@Button.BackButton.ToString()" value="Back" />
    <input id="NextButton" type="submit" name="@Button.NextButton.ToString()" value="Next" />
    <input id="FinishButton" type="submit" name="@Button.FinishButton.ToString()" value="Finish" />

} <-- `SQUIGGLY`


@{
    private enum Button
    {
        None,
        AddressButton,
        DetailsButton,
        MediaButton,
        NextButton,
        BackButton,
        FinishButton,
        CancelButton
    }

   public enum Step
   {
       Address,
       Details,
       Media,
       UpperBound
   }

   private const Step AddressStep = Step.Address;
   private const Step DetailsStep = Step.Details;
   private const Step MediaStep = Step.Media;
   private const Step First_Step = AddressStep;
   private const Step Last_Step = MediaStep;
   private const string CurrentStep = "CurrentStep";
   private const string DisabledAttribute = "disabled='disabled'";

   private string BackButtonState = string.Empty;
   private string NextButtonState = string.Empty;
   private string ErrorMessage = "Please correct the errors and try again.";
   private Button ButtonPressed = Button.None;
   private Step PreviousStepDisplayed = AddressStep;
   private Step StepToDisplay = AddressStep;
   private ModelStateDictionary[] StepModelState = new ModelStateDictionary[(int)Step.UpperBound];

   private void InitView()
   {
       // Create a ModelState for each individual step
        for (int key = (int)First_Step; key <= (int)Last_Step; key++)
        {
            StepModelState[key] = new ModelStateDictionary();
        }
   }

   /// <summary>
   /// Check form variables from the last HTTP_POST to figure where the wizard needs to be
   /// Grab the current step string along with which button was pressed
   /// </summary>
   private void AnalysePreviousState()
   {
       if (!string.IsNullOrEmpty(Request[CurrentStep]))
       {
           PreviousStepDisplayed = (Step)Enum.Parse(typeof(Step), Request[CurrentStep], true);
       }

       if (!string.IsNullOrEmpty(Request[Button.FinishButton.ToString()]))
       {
           ButtonPressed = Button.FinishButton;
       }

       if (!string.IsNullOrEmpty(Request[Button.CancelButton.ToString()]))
       {
           ButtonPressed = Button.CancelButton;
       }

       if (!string.IsNullOrEmpty(Request[Button.NextButton.ToString()]))
       {
           ButtonPressed = Button.NextButton;
       }

       if (!string.IsNullOrEmpty(Request[Button.BackButton.ToString()]))
       {
           ButtonPressed = Button.BackButton;
       }

       if (!string.IsNullOrEmpty(Request[Button.AddressButton.ToString()]))
       {
           ButtonPressed = Button.AddressButton;
       }

       if (!string.IsNullOrEmpty(Request[Button.DetailsButton.ToString()]))
       {
           ButtonPressed = Button.DetailsButton;
       }

       if (!string.IsNullOrEmpty(Request[Button.MediaButton.ToString()]))
       {
           ButtonPressed = Button.MediaButton;
       }
   }

   /// <summary>
   /// Sort all modelstate errors into the right step
   /// </summary>
   private void SortErrorsPerStep()
   {
       foreach (KeyValuePair<string, ModelState> entry in ViewData.ModelState)
       {
           foreach (int key in Enum.GetValues(typeof(Step)))
           {
               //Compare the start of each error's key with the name of a step
               //if they match then that error belongs to that step
               if (entry.Key.StartsWith(((Step)key).ToString()))
               {
                   StepModelState[key].Add(entry);
                   break;
               }
           }
       }

       ViewData.ModelState.Clear();
   }

   /// <summary>
   /// Look at the previous step to get any errors and which button was clicked
   /// and decide which step needs to be displayed now
   /// </summary>
   private void GetStepToDisplay()
   {
       //if the user tried to jump steps or finish, display the first step that has errors
       //this ensures that the wizard is completed in the intended sequence
       if (ButtonPressed != Button.NextButton && ButtonPressed != Button.BackButton)
       {
           ErrorMessage = "There are errors in the data provided. Please correct the errors and try again.";

           for (Step key = First_Step; key <= Last_Step; key++)
           {
               if (!StepModelState[(int)key].IsValid)
               {
                   DisplayStep(key, true);
                   return;
               }
           }
       }

       //if the last step has errors and the user has not hit the back button then stay on page and show the errors
       //user can go back through steps but not forward until errors are resolved
       if (!StepModelState[(int)PreviousStepDisplayed].IsValid && ButtonPressed != Button.BackButton)
       {
           DisplayStep(PreviousStepDisplayed, true);
           return;
       }

       //Otherwise move as per user request
       Step stepToDisplay = PreviousStepDisplayed;

       switch (ButtonPressed)
       {
           case Button.BackButton:
               stepToDisplay--;
               break;
           case Button.NextButton:
               stepToDisplay++;
               break;
           case Button.AddressButton:
               stepToDisplay = AddressStep;
               break;
           case Button.DetailsButton:
               stepToDisplay = DetailsStep;
               break;
           case Button.MediaButton:
               stepToDisplay = MediaStep;
               break;  
       }

       stepToDisplay = (Step)Math.Max((int)stepToDisplay, (int)First_Step);
       stepToDisplay = (Step)Math.Min((int)stepToDisplay, (int)Last_Step);

       DisplayStep(stepToDisplay, false);
   }

   private void DisplayStep(Step stepToDisplay, bool displayErrors)
   {
       StepToDisplay = stepToDisplay;
       BackButtonState = stepToDisplay == First_Step ? DisabledAttribute : string.Empty;
       NextButtonState = stepToDisplay >= Last_Step ? DisabledAttribute : string.Empty;

       //page number

       if (displayErrors)
       {
           foreach (KeyValuePair<string, ModelState> entry in StepModelState[(int)stepToDisplay])
           {
               ViewData.ModelState.Add(entry.Key, entry.Value);
           }
       }
   }


}

当我将鼠标悬停在 Viewbag 上时,我会看到通常的智能感知弹出窗口,将鼠标悬停在标题上会解释正常的“在运行时评估”。

有人遇到过这个问题吗?我查看了其他问题,但它们都有代码拼写错误或真正的错误。我知道这不是程序集引用问题,否则 Intellisense 不知道 Viewbag 是什么。

更新

看起来 Intellisense 有一个问题,特别是动态分配。普通服务器代码不会遇到上述问题,而像 Viewbag 分配或在 @{//code here} 中指定布局之类的东西似乎已损坏。另请注意,这仅发生在我的一个 .cshtml 文件中,其他文件不受影响。

更新 2 这是测试 View 的结果。源文件是在 ASP.NET 临时文件中生成的代码文件。

Compiler Error Message: CS1513: } expected

Source Error:

Line 259:            #line default
Line 260:            #line hidden
Line 261:WriteLiteral("\r\n");
Line 262:

这是编译临时文件中与上述错误相关的代码:

WriteLiteral(" value=\"Finish\"");

WriteLiteral(" /> \r\n");


    #line 46 "F:....Create.cshtml"

} <-- see the brace!? It's right there so why does the compiler freak out?

    #line default
    #line hidden
WriteLiteral("\r\n");            

最佳答案

你需要这个:

@switch (StepToDisplay)

它将闭合开关大括号视为与您之前的开关大括号的接近。您甚至可能需要将开关包含在 Razor 代码块中 @{ switch(){} }

顺便说一句,如果您使用的是 ReSharper,它会突出显示实际的代码块,并且您会看到 Razor 认为代码块结束的位置。

编辑:

它应该是这样的:

@using (Html.BeginForm())
{
    // remove the @ from before the Html on this line
    Html.Hidden("CurrentStep", StepToDisplay.ToString(), new { id="CurrentStep"})
    // This html causes razor to switch back to "html mode"
    <input id="AddressButton" type="submit" name="@Button.Step1Button.ToString()"...

    ...

    // Add an @ here, because razor is now in Html mode
    @switch (StepToDisplay)
    {
        ...
    }

    // Back in html mode again, so we need @'s
    <input id="BackButton" type="submit" name="@Button.BackButton.ToString()"...
    ...
}

关于asp.net-mvc-4 - Razor 看不到我的右括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16424888/

相关文章:

c# - 从类项目导入 Razor @helper

c# - 从 Grid MVC 提交多行

.net - 在 <link> 元素上使用 Razor 2 编写 RSS 失败

c# - Razor runco​​mpile 不允许我调试

asp.net-mvc - ASP.Net MVC 4 - 发现多个类型与名为 'Home' 的 Controller 匹配。

c# - ASP.NET MVC 4 Code-First IdentityUserRole 与额外的主键三向关系

c# - 在网格 MVC 中显示对象

c# - 使用 Ninject 的项目依赖项

asp.net-mvc-4 - MVC4 中 View 引擎的选择?

c# - 我已经开始学习 Entity Framework ,并面临问题。我有一个模型类,但无法建立 Entity Framework 连接