2014-11-05 90 views
-1

我现在每遇到这个问题&然后我永远不知道如何正确和轻松地修复它。所以请一劳永逸帮助我!我的form(cihForm)没有绑定到视图。一旦我加载视图,添加文本到两个输入框,并点击我的'保存'按钮调试器去我的“新建”构造函数在FormsViewModel>填充俱乐部和类别,因为它应该,一旦它完成了调试器在'控制器'中找到我的Create_Post函数。MVC中没有绑定的模型

一旦它进入HTTPpost动作中,我的模型就会有俱乐部和类别,但它的形式是空的。我的所有字段都没有分配给它的默认数据。

型号

Public Class FormsViewModel 
    Public form As cihForm = New cihForm() 
    Public Clubs As List(Of cihOrganizationSub) = New List(Of cihOrganizationSub) 
    Public Categories As List(Of cihCategoryOrgDef) = New List(Of cihCategoryOrgDef) 

    Public Sub New() 
     Dim lists As cihLists = New cihLists() 

     'Clubs 
     lists.loadOrganizationSubs(ZenCommon.CurrentOrgId, ZenCommon.CurrentUserId) 
     Clubs = New cihOrganizationSubList(ZenCommon.CurrentOrgId, ZenCommon.CurrentUserId).subList 

     'Categories 
     lists.loadCategoryOrgDef(ZenCommon.CurrentOrgId) 
     Categories = lists.organizationClubCategories 

    End Sub 
End Class 

控制器

<HttpGet> 
<ActionName("Create")> 
Function Create_Get() As ActionResult 
    Dim viewModel As FormsViewModel = New FormsViewModel() 
    Dim emptyList() As String = {} 

    'List of Orgs 
    ViewBag.SubOrgList = New SelectList(viewModel.Clubs, "subId", "codDescr", viewModel.form.selectedSubOrgId) 

    'List of Events 
    Dim search As cihEventSearch = New cihEventSearch(ZenCommon.CurrentOrgId, "", Date.Now.AddMonths(-1), Date.Now.AddMonths(1), True, emptyList, emptyList, emptyList) 
    ViewBag.EventList = New SelectList(search.eventList.OrderByDescending(Function(evt) evt.startUtc), "eventId", "eventTitle", viewModel.form.selectedEventId) 

    ViewBag.HideRightColumn = True 
    ViewBag.BodyClass = "span10" 

    Return View() 
End Function 

<HttpPost> 
<ActionName("Create")> 
Function Create_Post(form As FormsViewModel) As ActionResult 

    Return View() 
End Function 

查看

@ModelType MVCProject.FormsViewModel 
    @Using Html.BeginForm() 
     @Html.TextBoxFor(Function(model) model.form.Title, New With {.placeholder = "Title", .class = "hideOffFocus input-full large-type"}) 
     @Html.TextAreaFor(Function(model) model.form.Description, New With {.placeholder = "Description", .class = "hideOffFocus input-full"}) 

<input type="submit" name="cmdSave" value="Save" id="cmdSave" class="btn btn-primary" /> 
    End Using 

回答

-1

我的问题是,我忘了用模型中的“Property”标签来标记我的项目。我讨厌我必须为VB做这件事。

Public Class FormsViewModel 
    Public Property form As cihForm = New cihForm() 
    Public Property Clubs As List(Of cihOrganizationSub) = New List(Of cihOrganizationSub) 
    Public Property Categories As List(Of cihCategoryOrgDef) = New List(Of cihCategoryOrgDef) 

    Public Sub New() 
     Dim lists As cihLists = New cihLists() 

     'Clubs 
     lists.loadOrganizationSubs(ZenCommon.CurrentOrgId, ZenCommon.CurrentUserId) 
     Clubs = New cihOrganizationSubList(ZenCommon.CurrentOrgId, ZenCommon.CurrentUserId).subList 

     'Categories 
     lists.loadCategoryOrgDef(ZenCommon.CurrentOrgId) 
     Categories = lists.organizationClubCategories 

    End Sub 
End Class 
1

尝试使用模型绑定在您的查看

+0

我的看法是使用VB作为主要语言。 VB使用@ModelType,但谢谢各位 – MaylorTaylor 2014-11-06 14:22:34

+0

好吧,我认为这是C#。 – 2014-11-06 15:02:30