ASP.NETの押されたボタンを属性で判別するサンプルです。言語はVB.NETです。
目次
サンプル | ボタンのnameの値が異なる場合 |
ボタンのnameの値が同じ場合 |
ボタンのnameの値が異なる場合
ビュー
ボタンのnameの値が異なる場合です。nameの値で判定します。
@ModelType new0901.ActionForm1
@Code
Layout = Nothing
End Code
<!DOCTYPE html>
<html>
<head>
</head>
<body><p>@Model.text1</p>
@Using Html.BeginForm("Display1", "Test1", FormMethod.Post, New With {.id = "form1"})
@<input type="submit" name="Create" />
@<input type="submit" name="Delete" />
End Using
</body>
</html>
コントローラ
<>の箇所が押されたボタンを判別する属性です。
Imports new0901.Attribute
Namespace Controllers
Public Class Test1Controller
Inherits Controller
<HttpGet>
Function Index() As ActionResult
Dim actionform As New ActionForm1
actionform.text1 = "初期表示"
Return View("view1", actionform)
End Function
<HttpPost>
<ButtonTest(TestValue:="Create")>
Function Test1(actionform As ActionForm1) As ActionResult
actionform.text1 = "createです"
Return View("view1", actionform)
End Function
<HttpPost>
<ButtonTest(TestValue:="Delete")>
Function Test2(actionform As ActionForm1) As ActionResult
actionform.text1 = "deleteです"
Return View("view1", actionform)
End Function
End Class
End Namespace
7~11行目は、初期表示で使用しています。
14,21行目は、属性です。ButtonTestは、後述のButtonTestAttributeとひも付きます。
ビューのCreateボタンが押されたときは15行目、Deleteボタンが押されたときは22行目が実行されるようにします。
Attributeクラス
ActionNameSelectorAttributeクラスを継承します(5行目)。
9行目のIsValidNameメソッドはオーバーライドで設定されるので内容を記述します。
Imports System.Reflection
Namespace Attribute
<System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple:=True)>
Public Class ButtonTestAttribute
Inherits ActionNameSelectorAttribute
Public Property TestValue As String
Public Overrides Function IsValidName(
controllerContext As ControllerContext, actionName As String,
methodInfo As MethodInfo) As Boolean
IsValidName = False
Dim value =
controllerContext.Controller.ValueProvider.GetValue(TestValue)
If value Is Nothing = False Then
IsValidName = True
End If
Return IsValidName
End Function
End Class
End Namespace
コントローラに<ButtonTest~>が2つあるので7~19行目は、2回実行されます。
Createのボタンを押して7行目がCreateのとき、15行目でtrueになり16行目でtrueを返します。
次に7行目がDeleteのとき、15行目でfalseになりfalseを返します。
コントローラでは、15行目が実行されます。
フォーム
Public Class ActionForm1
Public Property text1 As String
End Class
ボタンのnameの値が同じ場合
ボタンのnameの値が同じ場合です。nameとvalueの値で判定します。
ビュー
@ModelType new0901.ActionForm1
@Code
Layout = Nothing
End Code
<!DOCTYPE html>
<html>
<head>
</head>
<body><p>@Model.text1</p>
@Using Html.BeginForm("Display1", "Test1", FormMethod.Post, New With {.id = "form1"})
@<input type="submit" name="button1" value="Create" />
@<input type="submit" name="button1" value="Delete" />
End Using
</body>
</html>
11,12行目は、nameの値が同じです。valueの値は異なります。
コントローラ
<>の箇所が押されたボタンを判別する属性です。
Imports new0901.Attribute
Namespace Controllers
Public Class Test1Controller
Inherits Controller
<HttpGet>
Function Index() As ActionResult
Dim actionform As New ActionForm1
actionform.text1 = "初期表示"
Return View("view1", actionform)
End Function
<HttpPost>
<ButtonTest(TestKey:="button1", TestValue:="Create")>
Function Test1(actionform As ActionForm1) As ActionResult
actionform.text1 = "createです"
Return View("view1", actionform)
End Function
<HttpPost>
<ButtonTest(TestKey:="button1", TestValue:="Delete")>
Function Test2(actionform As ActionForm1) As ActionResult
actionform.text1 = "deleteです"
Return View("view1", actionform)
End Function
End Class
End Namespace
7~11行目は、初期表示で使用しています。
14,21行目は、属性です。ButtonTestは、後述のButtonTestAttributeとひも付きます。
ビューのCreateボタンが押されたときは15行目、Deleteボタンが押されたときは22行目が実行されるようにします。
Attributeクラス
ActionNameSelectorAttributeクラスを継承します(5行目)。
9行目のIsValidNameメソッドはオーバーライドで設定されるので内容を記述します。
Imports System.Reflection
Namespace Attribute
<System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple:=True)>
Public Class ButtonTestAttribute
Inherits ActionNameSelectorAttribute
Public Property TestKey As String
Public Property TestValue As String
Public Overrides Function IsValidName(
controllerContext As ControllerContext, actionName As String,
methodInfo As MethodInfo) As Boolean
IsValidName = False
Dim value = controllerContext.HttpContext.Request.Form(TestKey)
If value Is Nothing = False Then
IsValidName = value.Equals(TestValue)
End If
Return IsValidName
End Function
End Class
End Namespace
コントローラに<ButtonTest~>が2つあるので7~19行目は、2回実行されます。
Createのボタンを押して7行目がbutton1で8行目がCreateのとき、15行目でtrueになり16行目でもtrueになります。
次に7行目がbuton1で8行目がDeleteのとき、15行目でtrueになり16行目でfalseになります。
コントローラでは、15行目が実行されます。
フォーム
Public Class ActionForm1
Public Property text1 As String
End Class
関連の記事
ASP.NET POSTで値を別画面に渡す(VB.NET)
ASP.NET JsonResultでJSONを返す(VB.NET)
ASP.NET RedirectToActionのサンプル(VB.NET)