将单选按钮绑定到MVC 3 VB.net中的对象

时间:2012-03-27 17:27:24

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-2

我浏览了很多网站和建议,但无法使绑定概念有效。我正面临以下问题。

我有以下Model类,如下所示: 主视图模型:

    Public Class MasterVM


    Public Property Language As LanguageVM
    Public Property Question As QuestionVM

    Public menus As List(Of MenuVM)

    Public Property UserControlName As String
    Public Property Model As Object

End Class

语言视图模型

 Public Class LanguageVM

#Region "Properties"

        Public Property IsEnglish As Boolean

#End Region


    End Class

我的控制器类:

Public Class WizardController
        Inherits System.Web.Mvc.Controller

        '
        ' GET: /Wizard
        Function Index() As ActionResult
            Dim oWizard As New Wizard
            Dim oMasterWizard As New MasterVM
            Dim IsNewlyIniated As Boolean = False
            If (System.Web.HttpContext.Current.Session(Constants.WizardObjectCollection) IsNot Nothing) Then
                If (System.Web.HttpContext.Current.Session(Constants.WizardObjectCollection).ToString <> "") Then
                    oMasterWizard = DirectCast(System.Web.HttpContext.Current.Session(Constants.WizardObjectCollection), MasterVM)
                Else
                    IsNewlyIniated = True
                End If
            Else
                IsNewlyIniated = True
            End If

            If (IsNewlyIniated) Then
                oMasterWizard = oWizard.GetWizardInfo()
                If (Session("Language").ToString() = "Spanish") Then
                    oMasterWizard.Wizard.Language.IsEnglish = False
                Else
                    oMasterWizard.Wizard.Language.IsEnglish = True
                End If

                System.Web.HttpContext.Current.Session(Constants.WizardObjectCollection) = oMasterWizard

            End If

            Return View("Wizard", oMasterWizard)

        End Function

 <HttpPost()>
        Function GetLanguageInfo(ByVal oLanguage As LanguageVM) As ActionResult
            Try
                Dim a As Integer
                a = 10
            Catch ex As Exception
                Throw ex
            End Try
        End Function

    End Class

我想在我的视图中显示两个单选按钮,显示英语和西班牙语。默认情况下,它应绑定到我的语言类isenglish和isspanish属性。

当帖子发生时,如果用户在Is selected属性中更改了它,我应该得到该值。我怎么能得到它。

我的ASCX页面代码如下:

<%@ Control Language="VB" Inherits="System.Web.Mvc.ViewUserControl(Of SBE.Services.MasterWizard.LanguageVM)" %>
<form method="post" action="/Wizard/GetLanguageInfo">
<fieldset>
    <legend>Wizard Language</legend>
    <p>Please select the language for your Wizard</p>

      <%= Html.RadioButtonFor(Function(model) model.IsEnglish, True)%>English  
<%= Html.RadioButtonFor(Function(model) model.IsEnglish, False)%> Spanish     
<input id="btnNext" type="submit" value="Next">  


    

另一个目标是,一旦我已经绑定,我想要在对象中选择用户选择的更新值并将其传递给控制器​​中定义的后期操作。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

我认为它没有绑定的原因是因为您在单选按钮中将对象的名称发布为“语言”,但它在LanguageVM中定义为“IsEnglish”。

为了简化操作,我建议您始终使用Html.RadioButtonFor,当您不需要在预期变量名称中使用硬编码时,错误的余地就会少得多。然后它应该像

<%= Html.RadioButtonFor(m => m.IsEnglish, true)%>English 
<%= Html.RadioButtonFor(m => m.IsEnglish, false)%> Spanish