首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MVC模型编辑不保存

MVC模型编辑不保存
EN

Stack Overflow用户
提问于 2015-10-09 20:48:38
回答 1查看 754关注 0票数 0

这与this other question I had previously posted有关联,也有一定的后续.

我在站点模型中添加了一个新属性,所以这就是现在的样子:

代码语言:javascript
复制
SiteID() As Integer
Name() As String
Latitude() As Double
Longitude() As Double
User() As ApplicationUser

如果您查看我在回答这个问题时的设备控制器--创建一个设备运行良好,所选择的站点将正确地保存到设备上。

但是,当我编辑设备并更改所选站点时,该更改将不会被保存。

在之前的一次修复中,我添加了一行来将站点从设备中删除,这就是Edit Post现在的样子:

代码语言:javascript
复制
    Async Function Edit(<Bind(Include:="DeviceID,Make,Model,Serial")> ByVal device As Device) As Task(Of ActionResult)
        If ModelState.IsValid Then
            'Because Site is selected in a DropDownFor which only passes the ID integer, need to set the device's site property accordingly
            'Note that with Site being a Navigation property, it looks like it doesn't get saved the way you would think...but it all works
            ModelState.Remove("Site")
            Dim thisSite As Integer = CInt(Request.Form.Get("Site.SiteID"))
            device.Site = (From site In db.Sites Where site.SiteID = thisSite Select site).FirstOrDefault()
            db.Entry(device).State = EntityState.Modified
            Await db.SaveChangesAsync()
            Return RedirectToAction("Index")
        End If
        Return View(device)
    End Function

不过,这并没有做到。当我遍历代码时,设备的站点确实得到了正确的设置,所以它看起来很好,但是索引get得到的是设备站点的旧值。

为什么这不适当地储蓄呢?

注意,在编辑中更改设备的其他属性确实很好。谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-14 19:12:41

如果格特·阿诺德说的话被贴上了答案,我就会表示接受了。我不知道是这样的!(如果它真的做到了,那就太好了)。但无论如何,下面是我最后做的事情的细节:

我必须向设备模型中添加一个属性才能保存SiteID --我称之为"SiteNum“。

在中,我只是根据下拉列表中选择的站点设置了SiteNum。

因为我想显示与设备关联的站点的名称,所以在Gets (创建除外)中,我使用device.Site设置SiteNum,更新表,然后在传递给视图的模型中包含站点。

作为这方面的一个例子,下面是我在Controller中的索引:

代码语言:javascript
复制
' GET: Devices
Async Function Index() As Task(Of ActionResult)
    'Have to set device's Site here in case user selected a different Site in Edit (which only updates scalar properties)
    Dim sitesList = db.Sites.Include("User").ToList
    'Can't call db.Sites within the For Each because it gives error about it already being open
    For Each item In db.Devices
        item.Site = sitesList.Where(Function(x) x.SiteID = item.SiteNum).FirstOrDefault()
        db.Entry(item).State = EntityState.Modified
    Next
    'Then have to update the database with the sites for the devices so the rest of this stuff will work
    Await db.SaveChangesAsync()
    Dim curUserID = User.Identity.GetUserId()
    'Need to have this include Site so that it will pull in that data in order to reference site name in the View
    '(doing this is called Eager Loading).  Only get devices for sites associated with logged in user.
    Dim devices = db.Devices.Include("Site").Where(Function(x) x.Site.User.Id = curUserID).ToListAsync()
    Return View(Await devices)
End Function

谢谢!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33046931

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档