2017-04-13 5 views
-1

ここは私のMVCビューページですVBコードで、そのViewData( "最大超過")はコントローラから渡されます。つまり、ViewData( "Maximum exceed")がtrueの場合、ラベルコントロールで行がレンダリングされます。私のVB構文に何か問題はありますか?どのように私はビューのページにVBの条件を置くことができますか?剃刀ビューページのVB構文

@Using (Html.BeginForm()) 
@<table> 

    @If ViewData("IsMaximumExceed") Then 
    <tr> 
     <td colspan="3"> 
      @Html.Label(
       Sub(settings) 
        settings.AssociatedControlName = "MaximumExceed" 
        settings.Text = "Maximum Exceed"                
       End Sub).GetHtml() 
     </td> 
    <tr> 
</table> 
End Using 
+1

問題は何ですか?それは私には大丈夫です... VBはASP.NET Core(Razorを含む)でサポートされていないので、できるだけ早くC#に移行する必要があります。 – Dai

+0

ASP.NET MVCには「コントロール」はありません.Html.LabelはHTMLをステートレスに直接描画する「HTMLヘルパー」です。 – Dai

+0

ひらめきビューの​​要素にVB構文エラーがありますが、何が問題なのですか? – Julia

答えて

0

@Using (Html.BeginForm())文は「コードブロックモード」でフォームを開始しますので、VB.NETのコンテキストで使用すると、「テキストモード」に設定する必要があるのいずれか、@<text>...</text>(単一または複数行モード)または単に@@:(単一ライン演算子)を使用して、 HTMLのブロック要素で続く:

' Using @: 
@Using (Html.BeginForm()) 
@:<table cellpadding="0" cellspacing="0" style="width: 800px; height:100px;"> 
    <!-- inner table --> 
@:</table> 
End Using 

' Using @<text>...</text> 
@Using (Html.BeginForm()) 
@<text><table cellpadding="0" cellspacing="0" style="width: 800px; height:100px;"> 
     <!-- inner table --> 
</table></text> 
End Using 

'Using @<tag>...</tag> 
@Using (Html.BeginForm()) 
    @<table cellpadding="0" cellspacing="0" style="width: 800px; height:100px;"> 
      <!-- inner table --> 
    </table> 
End Using 

HTMLタグを囲むように、オペレータの使用の背後にある理由は、VB.NETはC#はありません「テキストモード」内部「コード・ブロック・モード」を適用することにより、インラインXML要素を可能とすることですその機能(Razor syntax for VB.NETを参照)。

したがって、問題の提供サンプルはIfコードブロックの下にHTMLブロック要素を作成するために@<tr>前にタグを使用して、次のように変更する必要があります。

@Using (Html.BeginForm()) 
@<table cellpadding="0" cellspacing="0" style="width: 800px; height:100px;"> 
@If ViewData("IsGroupVessel") Then 
@<tr> 
     <td colspan="3"> 
     @Html.DevExpress().Label(Sub(settings) 
      settings.AssociatedControlName = "GroupBooking" 
      settings.Text = "Maximum Exceeded"                
     End Sub).GetHtml() 
     </td> 
    </tr> 
    End If 
</table> 
End Using 

@Daiが彼の答えで述べたように、私も好みます特性を有する、強く型付けされた「ビューモデル」の使用ではなく、従って変更されたコードを以下に示すViewBag又はViewDataを通過:

モデル

Public Class ViewModel 
    Public Property IsExceeded As Boolean 
    ' other models 
End Class 

コントローラ

Public Function ActionMethod() As ActionResult 
    Dim model As New ViewModel 
    ' other logic 
    model.IsExceeded = True 
    ' other logic 
    Return Me.View(model) 
End Function 

ビュー

@ModelType ViewModel 

@Using (Html.BeginForm()) 
@<table cellpadding="0" cellspacing="0" style="width: 800px; height:100px;"> 
@If Model.IsExceeded Then 
@<tr> 
     <td colspan="3"> 
     @Html.DevExpress().Label(Sub(settings) 
      settings.AssociatedControlName = "GroupBooking" 
      settings.Text = "Maximum Exceeded"                
     End Sub).GetHtml() 
     </td> 
    </tr> 
    End If 
    </table> 
End Using 

関連問題:

Razor View Engine Quirks in VB.NET

Using in Razor VB.net MVC not work as expected

+0

私はそれを得ました、これはまさに私が探していたものです。本当にありがとう。 – Julia

+0

@Juliaスタックオーバーフローへようこそ。その解決策を見つけたことを示すチェックマークをクリックすることで、問題を解決した回答を[受諾](https://meta.stackexchange.com/q/5234/179419)とみなしてください。そうする義務はありません。 –

0

あなたは引数がLabel(HtmlHelper, String, String)に間違っています。最初の文字列パラメータはexpression、2番目の文字列パラメータはlabelTextです。そこキー"Maximum exceed"と辞書メンバーだがexpression値が"MaximumExceed"ある場合

あなたIf ViewData("Maximum exceed")かどうかを確認する - 彼らは一致していません。

つまり、型なしViewDataまたはViewBagオブジェクトを使用する代わりに、厳密に型指定された「ビューモデル」オブジェクトを使用することをお勧めします。

Public Class MyPageViewModel 

    Public MaximumExceeded As Boolean 

End Class 

... 

Public Function MyControllerAction() As ActionResult 

    Dim viewModel As New MyPageViewModel 
    viewModel.MaximumExceeded = True 
    Return Me.View(viewModel) 

End Sub 

... 

@Model MyPageViewModel 

@If Model.MaximumExceeded Then 

    @Html.Label(NameOf(Model.MaximumExceeded), "Maximum exceeded") 

End If 

は、プロパティの文字列名を取得するNameOfオペレータの私の使用に注意してください。または、より良い方法は、Html.LabelForを使用します。

+0

お返事ありがとうございました。私はかみそりの視点で表現を適用します。 – Julia

+0

しかし、テーブル行にLabelコントロールを表示したいのですが、ビューにVB構文条件を挿入するにはどうすればいいですか? – Julia

+0

@If ViewDataを次に END IF – Julia

関連する問題
@ Html.Label(がNameOf(Model.MaximumExceeded)、 "最大超過")