Razor MVC中的模型未绑定

Razor MVC中的模型未绑定,razor,model-view-controller,binding,model-binding,Razor,Model View Controller,Binding,Model Binding,我时不时都会遇到这个问题,我从来不知道如何正确、轻松地解决它。所以,请帮我一劳永逸 我的表单(cihForm)未绑定到视图。加载视图后,将文本添加到两个输入框中,然后单击我的“保存”按钮。调试器将转到表单viewmodel>中的“新建”构造函数,并按应填充俱乐部和类别,一旦完成,调试器将转到“控制器”中的我的创建帖子函数 一旦它进入HTTPpostaction,我的模型就应该有俱乐部和类别,但是表单是空的。我的所有字段都没有指定任何内容或默认数据 型号 Public Class FormsVie

我时不时都会遇到这个问题,我从来不知道如何正确、轻松地解决它。所以,请帮我一劳永逸

我的
表单
(cihForm)未绑定到视图。加载视图后,将文本添加到两个输入框中,然后单击我的“保存”按钮。调试器将转到
表单viewmodel
>中的“新建”构造函数,并按应填充俱乐部和类别,一旦完成,调试器将转到“控制器”中的我的
创建帖子
函数

一旦它进入
HTTPpost
action,我的模型就应该有俱乐部和类别,但是表单是空的。我的所有字段都没有指定任何内容或默认数据

型号

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

函数Create_Get()作为ActionResult
Dim viewModel As FormsViewModel=新FormsViewModel()
Dim emptyList()作为字符串={}
"组织名单"
ViewBag.SubOrgList=新选择列表(viewModel.Clubs,“subId”,“codDescr”,viewModel.form.selectedSubOrgId)
"活动清单"
Dim搜索为cihEventSearch=New cihEventSearch(zencomon.CurrentOrgId,“”,Date.Now.AddMonths(-1),Date.Now.AddMonths(1),True,emptyList,emptyList,emptyList)
ViewBag.EventList=新建SelectList(search.EventList.OrderByDescending(函数(evt)evt.startUtc)、“eventId”、“eventTitle”、viewModel.form.selectedEventId)
ViewBag.HideRightColumn=True
ViewBag.BodyClass=“span10”
返回视图()
端函数
函数Create_Post(表单为FormsViewModel)作为ActionResult
返回视图()
端函数
查看

    @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
@ModelType MVCProject.FormsViewModel
@使用Html.BeginForm()
@Html.TextBoxFor(Function(model)model.form.Title,新的,带有{.placeholder=“Title”,.class=“hideOffFocus input full-large-type”})
@Html.TextAreaFor(Function(model)model.form.Description,新增为{.placeholder=“Description”,.class=“hideOffFocus input full”})
终端使用

尝试在您的视图中使用
@model MVCProject.FormsViewModel
绑定模型

我的问题是,我忘了在模型中为我的项目添加“属性”标签。我讨厌我必须为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

我的观点是使用VB作为主要语言。VB使用@ModelType,不过还是要谢谢你