2013-05-07 42 views
8

我不確定爲什麼這個語法會出現錯誤「輸入未聲明,可能因防護級別而無法訪問」,必須將「@html(」錯誤。在Razor中使用VB.net MVC不能正常工作

此塊抱怨錯誤

@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" /> 

enter image description here

喜nemesv如果使用@<Text>
,它的抱怨這一點 - 「使用必須結束而終止使用」> enter image description here

+0

你已經忘記關閉@'在我的例子中看到我有:'@'注意關閉''該線。 – nemesv 2013-05-08 07:28:59

+0

反對!在MVC中太新了。一切似乎都對我來說很新鮮,謝謝! – tsohtan 2013-05-08 07:30:27

+0

[爲什麼@Using Html.BeginForm中的普通標記需要@?](http://stackoverflow.com/questions/10225394/why-does-normal-markup-inside-an-using-html- beginform-need-an) – KyleMit 2014-11-10 21:58:23

回答

8

當你是一個Using塊裏面你是在剃刀「代碼模式」 。

因此,您需要使用@:(對於單行語句)或@<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 

參見進一步信息的Combining text, markup, and code in code blocks部分。

+0

「@」在「」前面是一個VB事物嗎?我知道它並不需要它C# – mattytommo 2013-05-07 08:45:25

+1

@mattytommo是'@'是C#中的VB唯一的東西,你只需要'' – nemesv 2013-05-07 08:53:09

+0

我仍然有這個錯誤:'>'在瀏覽器中運行時預期。在這一行@:但是在設計時沒關係,並且@不適合我。我正在使用vs 2012. .net 4.5 – tsohtan 2013-05-08 07:07:57