【发布时间】:2012-03-09 01:31:27
【问题描述】:
我正在尝试在应用程序运行时以编程方式在我的模型中创建属性。我试图在这篇帖子How to create controls dynamically in MVC 3 based on an XML file
上关注 Darin Dimitrov 的回答我正在尝试将代码转换为 VB.NET。到目前为止,我...
型号:
Public Class MyViewModel
Public Property Controls As ControlViewModel()
End Class
Public MustInherit Class ControlViewModel
Public MustOverride ReadOnly Property Type As String
Public Property Visible As Boolean
Public Property Label As String
Public Property Name As String
End Class
Public Class TextBoxViewModel
Inherits ControlViewModel
Public Overrides ReadOnly Property Type As String
Get
Return "textbox"
End Get
End Property
Public Property Value As String
End Class
Public Class CheckBoxViewModel
Inherits ControlViewModel
Public Overrides ReadOnly Property Type As String
Get
Return "checkbox"
End Get
End Property
Public Property Value As Boolean
End Class
控制器:
Function Test() As ActionResult
Dim model = New MyViewModel() With { _
.Controls = New ControlViewModel() {New TextBoxViewModel() With { _
.Visible = True, _
.Label = "text label", _
.Name = "TextBox1", _
.Value = "Text appears here" _
}, New CheckBoxViewModel() With { _
.Visible = True, _
.Label = "check label", _
.Name = "CheckBox1", _
.Value = True _
}
}}
Return View("Test", model)
End Function
<httpPost()>
Function Test(model As MyViewModel) As ActionResult
Return View("Test", model)
End Function
查看:
@ModelType MyApp.DomainModel.MyTest.MyViewModel
@Code
Using Html.BeginForm()
Dim i As Integer
For i = 0 To Model.Controls.Length - 1
End Code
<div>
@Html.EditorFor(Function(model) model.Controls(i))
</div>
@Code
Next
End Code
<input type="submit" value="OK" />
@Code
End Using
End Code
文本框编辑器模板:
@modeltype MyApp.DomainModel.MyTest.TextBoxViewModel
@Html.LabelFor(Function(model) model.Value, Model.Label)
@Html.TextBoxFor(Function(model) model.Value)
复选框编辑器模板:
@modeltype MyApp.DomainModel.MyTest.CheckBoxViewModel
@Html.LabelFor(Function(model) model.Value, Model.Label)
@Html.CheckBoxFor(Function(model) model.Value)
自定义模型绑定器:
Public Class ControlModelBinder
Inherits DefaultModelBinder
Public Overrides Function BindModel(controllerContext As ControllerContext, bindingContext As ModelBindingContext) As Object
Dim type = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + ".Type")
Dim name = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + ".Name")
Dim value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + ".Value")
If type IsNot Nothing AndAlso value IsNot Nothing Then
Select Case type.AttemptedValue
Case "textbox"
Return New TextBoxViewModel() With { _
.Name = name.AttemptedValue, _
.Value = value.AttemptedValue _
}
Case "checkbox"
Return New CheckBoxViewModel() With { _
.Name = name.AttemptedValue, _
.Value = Boolean.Parse(value.AttemptedValue.Split(","c).First()) _
}
End Select
End If
Throw New NotImplementedException()
End Function
End Class
全球.asax:
ModelBinders.Binders.Add(GetType(MyTest.ControlViewModel), New MyTest.ControlModelBinder())
当我运行应用程序时,自定义模型绑定器中的类型和名称变量似乎没有正确设置。
我做错了什么?
【问题讨论】:
标签: asp.net-mvc-3