ASP.NET MVC 使用 UpdateModel 发布子记录

发布于 2024-07-16 03:52:18 字数 1070 浏览 5 评论 0原文

继续这个问题,我有一个表单中列出了某个人的所有车辆,车辆字段是可编辑的,并且它们现在已成功发回我的“保存”操作。

现在我想使用 UpdateModel 来保存数据,但我不知道如何构建它。 这是我现在的“保存”操作:

<ActionName("Edit"), AcceptVerbs(HttpVerbs.Post)> _
Function Save(ByVal form As Person, ByVal vehicles() As Vehicle) As ActionResult
    Dim original = From v In MyDataContext.Vehicles Where v.person_id = Person.person_id
    For Each item In original
        For i = 0 To vehicles.Count - 1
            If vehicles(i).vehicle_id = item.vehicle_id Then
                UpdateModel(item, New String() {"license_nbr", "color"})
                Exit For
            End If
        Next
    Next
    MyDataContext.SubmitChanges()
    Return RedirectToAction("Index", "Home")
End Function

当我运行此操作时,它不会保存任何内容,并且 UpdateModel 不会引发任何错误。 我假设我必须给它更多的指导才能让它发挥作用,因为 UpdateModel 不知道每次更新要使用车辆数组中的哪个项目。

我是否需要指定 ValueProviderResult 作为 UpdateModel 的第三个参数? 如果是这样,我如何从车辆(i)中创建一辆? 我的设置方式是否完全偏离了基础?

Continuing from this question, I've got a form with all the Vehicles listed for a Person, the vehicle fields are editable, and they are now successfully posting back to my Save action.

Now I'd like to use UpdateModel to save the data, but I'm not sure how to construct it. Here's my Save action right now:

<ActionName("Edit"), AcceptVerbs(HttpVerbs.Post)> _
Function Save(ByVal form As Person, ByVal vehicles() As Vehicle) As ActionResult
    Dim original = From v In MyDataContext.Vehicles Where v.person_id = Person.person_id
    For Each item In original
        For i = 0 To vehicles.Count - 1
            If vehicles(i).vehicle_id = item.vehicle_id Then
                UpdateModel(item, New String() {"license_nbr", "color"})
                Exit For
            End If
        Next
    Next
    MyDataContext.SubmitChanges()
    Return RedirectToAction("Index", "Home")
End Function

When I run this, it doesn't save anything, and UpdateModel doesn't throw any errors. I'm assuming I have to give it a little more direction to get the magic to work, because UpdateModel doesn't know which item in the vehicles array to use for each update.

Do I need to specify a ValueProviderResult as the third parameter to UpdateModel? If so, how do I create one out of vehicles(i)? Am I completely off base in how I have this set up?

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

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

发布评论

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

评论(1

李不 2024-07-23 03:52:18

当您已经将表单字段处理为模型数据时,为什么要使用 UpdateModel(它只是更新表单字段的属性)? 您不能直接将 vehicle 中的值分配给 item 吗?

For Each item In original
    For i = 0 To vehicles.Count - 1
        If vehicles(i).vehicle_id = item.vehicle_id Then 
           item.license_nbr = vehicles(i).license_nbr
           item.color = vehicles(i).color
           Exit For
        End If
    Next
Next

Why use UpdateModel -- which just updates the properties from form fields -- when you already have the form fields processed into model data? Can't you just assign the values from vehicle to item directly?

For Each item In original
    For i = 0 To vehicles.Count - 1
        If vehicles(i).vehicle_id = item.vehicle_id Then 
           item.license_nbr = vehicles(i).license_nbr
           item.color = vehicles(i).color
           Exit For
        End If
    Next
Next
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文