2011-01-18 12 views
1

私はASP.NET MVC2のWebサイトで作業しています。学習のために。私はVisual Basic言語で作業しています。ビューでカスタムASP.NET MVC2 HtmlHelper:渡されたオブジェクトのインスタンスを取得するには?

私はこれやりたい:

Imports System.Runtime.CompilerServices 
Imports System.Linq.Expressions 
Imports System.Reflection 

Public Module HtmlHelpers 
    <Extension()> _ 
    Public Function EditorForEnumeracion(Of TModel, TValue)(ByVal html As HtmlHelper(Of TModel), ByVal expression As Expression(Of Func(Of TModel, TValue))) As MvcHtmlString 
     'My stuff goes here 
    End Function 
End Module 

問題は、私はインスタンスを取得する方法がわからないということです。だから、

<%: Html.EditorForEnumeracion(Function(v) v.combustible%> 

を、私はHtmlHelperの拡張メソッドを作成しました私がヘルパーに渡すv.combustibleオブジェクトの私はvオブジェクトを気にしない。私はvオブジェクトの可燃属性で作業する必要があります。

Herehereこれはどうやっているようですがわかりません。また、私はC#ではなくVisual Basicで作業します。

私は式パラメータを使ってEnumeracionオブジェクトのインスタンスを取得できると思いますが、どのように理解できません。

詳細情報

これが私の "Vehiculo" クラスです:

Namespace Models.Automovil 
    Public Class Vehiculo 
     Public Property tipo As New Models.Enumeracion("TipoDeVehiculo") 
     Public Property marca As String 
     Public Property modelo As String 
     Public Property numeroDePuertas As Integer 
     Public Property combustible As New Models.Enumeracion("TipoDeCombustible") 
     Public Property potencia As Integer 
     Public Property fechaPrimeraMatriculacion As DateTime 
     Public Property version As String 
     Public Property precio As Decimal 
     Public Property descripcion As String 
    End Class 
End Namespace 

そして、これは私の "Enumeracion" クラスです:

Namespace Models 
    Public Class Enumeracion 
     Private bd As New tarificadorasegasaEntities 
     Private diccionario As New Dictionary(Of String, Integer) 
     Private _nombre As String 
     Private _clave As String 
     Private _valor As Integer 
     Public ReadOnly Property nombre As String 
      Get 
       Return _nombre 
      End Get 
     End Property 
     Public ReadOnly Property clave As String 
      Get 
       Return _clave 
      End Get 
     End Property 
     Public ReadOnly Property valor As Integer 
      Get 
       Return _valor 
      End Get 
     End Property 

     'More stuff here. Methods. 

    End Class 
End Namespace 

モデルはVehiculoクラスです。

まだ解決していません。

ありがとうございます。

答えて

0

このようにしてみてください。

<Extension()> _ 
Public Function EditorForMyCustomClassB(Of Vehiculo, Enumeracion)(ByVal html As HtmlHelper(Of TModel), ByVal expression As Expression(Of Func(Of Vehiculo, Enumeracion))) As MvcHtmlString 
    Dim res = ModelMetadata.FromLambdaExpression(expression, html.ViewData) 
    Dim e As Enumeracion = DirectCast(res.Model, Enumeracion) 
    ' use e here 
End Function 
+0

res.ModelはNothingなので、eもNothingです。 – Daniel

+0

@Daniel、res.ModelがNothingの場合は、ビューが強く型付けされていないか、コントローラアクションからモデルを渡していないことを意味します。 –

+0

あなたは正しいです。どうもありがとうございました。 – Daniel

1

あなたは、モデル上でそれを呼び出す、Func(Of TModel, TValue)に表現をコンパイルする必要があります。

Dim func = expression.Compile() 
Dim value = func(html.ViewData.Model) 
+0

ときFUNC(html.ViewData.Model私はNullFeferenceExceptionを得ました)。なぜなのかご存知ですか? お時間をいただきありがとうございます。 – Daniel

+0

あなたのモデルは何ですか? – SLaks

+0

モデルは私のVehiculoクラスです。 – Daniel

関連する問題