2014-11-05 65 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