-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHomeController.vb
81 lines (69 loc) · 3.12 KB
/
HomeController.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web.Mvc
Imports System.Web.Script.Serialization
Imports GridViewOptimisticConcurrencyMvc.Models
Namespace GridViewOptimisticConcurrencyMvc.Controllers
Public Class HomeController
Inherits Controller
Private db As CustomerDbContext = New CustomerDbContext()
Public Function Index() As ActionResult
Return View()
End Function
Public Function GridViewPartial() As ActionResult
Return PartialView(db.Customers.ToList())
End Function
Public Function GridViewPartialAddNew(ByVal customer As Customer) As ActionResult
Dim model = db.Customers
If ModelState.IsValid Then
Try
db.Entry(customer).State = Data.Entity.EntityState.Added
db.SaveChanges()
Catch e As Exception
ViewData("EditError") = e.Message
End Try
Else
ViewData("EditError") = "Please, correct all errors."
End If
Return PartialView("GridViewPartial", db.Customers.ToList())
End Function
Public Function GridViewPartialUpdate(ByVal customer As Customer) As ActionResult
Dim model = db.Customers
customer.RowVersion = CalculateOldRowVersion(customer.Id)
If ModelState.IsValid Then
Try
db.Entry(customer).State = Data.Entity.EntityState.Modified
db.SaveChanges()
Catch e As Exception
ViewData("EditError") = e.Message
End Try
Else
ViewData("EditError") = "Please, correct all errors."
End If
Return PartialView("GridViewPartial", db.Customers.ToList())
End Function
Public Function GridViewPartialDelete(ByVal customer As Customer) As ActionResult
Dim model = db.Customers
customer.RowVersion = CalculateOldRowVersion(customer.Id)
If ModelState.IsValid Then
Try
db.Entry(customer).State = Data.Entity.EntityState.Deleted
db.SaveChanges()
Catch e As Exception
ViewData("EditError") = e.Message
End Try
Else
ViewData("EditError") = "Please, correct all errors."
End If
Return PartialView("GridViewPartial", db.Customers.ToList())
End Function
Private Function CalculateOldRowVersion(ByVal id As Integer) As Byte()
Dim serializer As JavaScriptSerializer = New JavaScriptSerializer()
Dim rowVersions As String = Request("RowVersions")
Dim dictionary As Dictionary(Of Object, String) = CType(serializer.Deserialize(rowVersions, GetType(Dictionary(Of Object, String))), Dictionary(Of Object, String))
Dim rowVersion As Char() = dictionary(id.ToString()).ToCharArray()
Return Convert.FromBase64CharArray(rowVersion, 0, rowVersion.Length)
End Function
End Class
End Namespace