Asp.net-Mvc

在 Razor VB.net MVC 中使用無法按預期工作

  • May 8, 2013

我不確定為什麼此語法會抱怨錯誤“未聲明 Enter。由於保護級別可能無法訪問”,並且必須輸入“@html(”才能消除該錯誤。

此塊抱怨錯誤

  @Using (Html.BeginForm("GetUser", "UserProfile", FormMethod.Post))
     Enter User id :-  @Html.TextBox("UserId",Model)  -- This line must write in this way @Html("Enter User id :-")
     <input type="submit" value="Submit  data" />  --This line complain ">" expected"
  End Using 

如果以這種方式重寫程式碼,則抱怨消失了,但輸出開頭顯示“System.Web.MVC.Html”,如下圖所示

      @Html.BeginForm("GetUser", "UserProfile", FormMethod.Post)
      Enter User id :-   @Html.TextBox("UserId",Model) 

   <input type="submit" value="Submit  data" />

在此處輸入圖像描述

嗨 nemesv 如果使用@<Text>

,它會抱怨這個 –>“使用必須以結束使用結束。” 在此處輸入圖像描述

當你在一個Using塊內時,你在 Razor 中處於“程式碼模式”。

所以你需要使用@:(對於單行語句)或者@<text> .. </text>(對於多行語句)切換回“文本模式”並輸出html。

隨著使用@:

@Using (Html.BeginForm("GetUser", "UserProfile", FormMethod.Post))
     @:Enter User id :-  @Html.TextBox("UserId",Model)  
     @:<input type="submit" value="Submit  data" />
End Using

或使用@<text>

@Using (Html.BeginForm("GetUser", "UserProfile", FormMethod.Post))
     @<text>Enter User id :-</text>  @Html.TextBox("UserId",Model)  
     @<text><input type="submit" value="Submit  data" /></text>
End Using

另請參閱在程式碼塊中組合文本、標記和程式碼部分以獲取更多資訊。

引用自:https://stackoverflow.com/questions/16414271