在 html.beginform 中传递嵌套视图模型

发布于 2024-11-06 23:24:03 字数 2682 浏览 0 评论 0原文

我有一个问题,我有一个带有嵌套视图模型(ChildViewModel)的视图模型(MasterViewModel),并且我在表单中有一个下拉列表,它通过 jQuery 执行 ajax post 以更新部分视图。但无论我做什么,Part 操作中的 childviewmodel 总是为空,我不明白为什么。这是一个简化的代码示例:

控制器:

Namespace MvcApplication2
    Public Class TestController
        Inherits System.Web.Mvc.Controller

        '
        ' GET: /Test

        Function Index() As ActionResult
            Dim viewModel As New MasterViewModel
            viewModel.MyId = 123
            viewModel.ChildViewModel = New ChildViewModel With {.Name = "woohoo"}
            viewModel.Items = New List(Of SelectListItem) From { _
                New SelectListItem With {.Text = "first", .Value = "1"},
                New SelectListItem With {.Text = "second", .Value = "2"}
            }
            viewModel.SelectedItem = New List(Of SelectListItem) From { _
                New SelectListItem With {.Text = "first", .Value = "1"}
            }
            Return View(viewModel)
        End Function

        Function Part(ByVal viewModel As ChildViewModel) As ActionResult
            viewModel.Name = viewModel.Name & "_"
            Return PartialView(viewModel)
        End Function


    End Class
End Namespace

索引视图:

@ModelType MvcApplication2.MasterViewModel
@Code
    ViewData("Title") = "Index"
End Code
<script type="text/javascript" language="javascript">
    function update() {
        $('#myForm').change(function () {
            var form = $('#myForm');
            $.ajax({
                url: form.attr('action'),
                type: form.attr('method'),
                data: form.serialize(),
                success: function (result) {
                    $('#mjoo').html(result);
                }
            });
        });
    }

</script>
<h2>Index</h2>

<p>@Model.MyId</p>

@Using Html.BeginForm("Part", "Test", FormMethod.Post, New With {.id = "myForm"})

    @Html.DropDownListFor(Function(x) x.SelectedItem, Model.Items, New With {.onchange = "update()"})
    @Html.HiddenFor(Function(x) x.ChildViewModel)

End Using

<div id="mjoo">

    @Html.Partial("Part", Model.ChildViewModel)

</div>

零件视图:

@ModelType MvcApplication2.ChildViewModel

<p>@Model.Name</p>

Viewmodels:

Public Class MasterViewModel
    Property ChildViewModel As ChildViewModel
    Property Items As List(Of SelectListItem)
    Property SelectedItem As List(Of SelectListItem)
    Property MyId As Integer
End Class

Public Class ChildViewModel
    Property Name As String
End Class

这可能有什么问题。我知道我错过了一些东西,但我就是看不到它。

I have the issue that i have a viewmodel (MasterViewModel) with a nested viewmodel (ChildViewModel) and i have a dropdownlist within a form, that does an ajax post via jQuery in order to update a partial view. But no matter what i do, the childviewmodel in the Part action is always empty, and I dont understand why. Heres a simplified code example:

Controller:

Namespace MvcApplication2
    Public Class TestController
        Inherits System.Web.Mvc.Controller

        '
        ' GET: /Test

        Function Index() As ActionResult
            Dim viewModel As New MasterViewModel
            viewModel.MyId = 123
            viewModel.ChildViewModel = New ChildViewModel With {.Name = "woohoo"}
            viewModel.Items = New List(Of SelectListItem) From { _
                New SelectListItem With {.Text = "first", .Value = "1"},
                New SelectListItem With {.Text = "second", .Value = "2"}
            }
            viewModel.SelectedItem = New List(Of SelectListItem) From { _
                New SelectListItem With {.Text = "first", .Value = "1"}
            }
            Return View(viewModel)
        End Function

        Function Part(ByVal viewModel As ChildViewModel) As ActionResult
            viewModel.Name = viewModel.Name & "_"
            Return PartialView(viewModel)
        End Function


    End Class
End Namespace

Index View:

@ModelType MvcApplication2.MasterViewModel
@Code
    ViewData("Title") = "Index"
End Code
<script type="text/javascript" language="javascript">
    function update() {
        $('#myForm').change(function () {
            var form = $('#myForm');
            $.ajax({
                url: form.attr('action'),
                type: form.attr('method'),
                data: form.serialize(),
                success: function (result) {
                    $('#mjoo').html(result);
                }
            });
        });
    }

</script>
<h2>Index</h2>

<p>@Model.MyId</p>

@Using Html.BeginForm("Part", "Test", FormMethod.Post, New With {.id = "myForm"})

    @Html.DropDownListFor(Function(x) x.SelectedItem, Model.Items, New With {.onchange = "update()"})
    @Html.HiddenFor(Function(x) x.ChildViewModel)

End Using

<div id="mjoo">

    @Html.Partial("Part", Model.ChildViewModel)

</div>

Part view:

@ModelType MvcApplication2.ChildViewModel

<p>@Model.Name</p>

Viewmodels:

Public Class MasterViewModel
    Property ChildViewModel As ChildViewModel
    Property Items As List(Of SelectListItem)
    Property SelectedItem As List(Of SelectListItem)
    Property MyId As Integer
End Class

Public Class ChildViewModel
    Property Name As String
End Class

What might be wrong with this. I know there is something i'm missing, but I just can't see it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃酥萝莉 2024-11-13 23:24:03

您无法神奇地将整个模型填充到单个字段中。
您需要为模型中的每个属性创建一个单独的隐藏字段。

我建议将整个表单移动到一个单独的部分视图,该视图采用子模型来简化代码。

You cannot magically stuff an entire model into a single field.
You need to make a separate hidden field for each property in the model.

I recommend moving th the entire form to a separate partial view that takes the child model to simplify the code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文