2012-03-27 37 views
1

我经历了许多网站和建议,但无法使绑定概念正常工作。我面临以下问题。将单选按钮绑定到MVC中的对象3 VB.net

我有以下型号类,如下: 主视图模型:

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属性。

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

我的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

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

为了使事情更简单,我建议您始终使用Html.RadioButtonFor,如果您不必在预期变量的名称中进行硬编码,那么出错的可能性就会小得多。然后,它应该是这样

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

我试图上面的代码和在视图源I可以看到以下代码:<输入的ID =“IsEnglish”名称=“IsEnglish”类型=“无线电”的值=“真” />英文西班牙文。但由于某种原因,我的帖子操作不会在下一个按钮点击时激怒,以便知道该值是否被绑定。任何建议 - – 2012-03-27 19:50:17

+0

谢谢芬克尔斯坦它的工作。我在帖子中更新了上述解决方案。 – 2012-03-27 20:01:12