如何使用 visual basic 和 mvc core 从按钮调用函数
How to call function from button using visual basic and mvc core
我想在 visual studio 2019 年使用 Visual Basic 和 MVC 创建一个调用方法的按钮,
当我尝试将其设为按钮时,没有响应
视图代码和控制器代码下方
P.S。 OPenCaseController 是我的控制器,init() 是我想使用按钮调用的函数
@Code
Layout = Nothing
End Code
<!DOCTYPE html>
<html>
<head>
<meta name = "viewport" content="width=device-width" />
<title>init</title>
</head>
<body>
<div Class="form-group">
<form method="post" action="OPEN">
<Button type="button" onclick="location.href='@Url.Action("OPenCaseController", "init")'"> OPENhysys </Button>
<input type="text" name="x" />
</form>
</div>
</body>
</html>
这里是使用 visual basic 的控制器代码
Imports System.Web.Mvc
Imports Aspentech
Imports Aspentech.HYSYS
Namespace Controllers
Public Class OPenCaseController
Inherits Mvc.Controller
Dim hyCase As SimulationCase
Dim hyApp As Aspentech.HYSYS.Application
Function init() As ActionResult
ViewData("Message") = "Your contact page."
Return View()
End Function
<HttpPost()>
<ActionName("OPEN")>
Public Function init(x) As ActionResult
ViewData("Message") = "Your contact page."
hyApp = CreateObject(“HYSYS.Application”)
hyCase = hyApp.SimulationCases.Open(“E:\otts\st st trial-reem.hsc”)
hyCase.Activate()
Return View()
End Function
End Class
End Namespace
<Button type="button" onclick="location.href='@Url.Action("OPenCaseController", "init")'"> OPENhysys </Button>
问题与@Url.Action()
方法有关,如果右键单击该方法并选择“转到定义”,您可以看到第一个参数应该是动作名称,然后第二个参数是控制器的名称,如下所示(屏幕截图是从 C# 应用程序中捕获的):
有关 @Url.Action()
方法的更多详细信息,请查看 this document。
此外,在MVC应用程序中,即使控制器文件名是“OPenCaseController”,控制器的名称也应该是“OPenCase”。有关 MVC 应用程序的更多详细信息,请查看 this tutorial.
所以,试着把上面的代码改成下面这样:
<Button type="button" onclick="location.href='@Url.Action("init", "OPenCase")'"> OPENhysys </Button>
我想在 visual studio 2019 年使用 Visual Basic 和 MVC 创建一个调用方法的按钮, 当我尝试将其设为按钮时,没有响应
视图代码和控制器代码下方 P.S。 OPenCaseController 是我的控制器,init() 是我想使用按钮调用的函数
@Code
Layout = Nothing
End Code
<!DOCTYPE html>
<html>
<head>
<meta name = "viewport" content="width=device-width" />
<title>init</title>
</head>
<body>
<div Class="form-group">
<form method="post" action="OPEN">
<Button type="button" onclick="location.href='@Url.Action("OPenCaseController", "init")'"> OPENhysys </Button>
<input type="text" name="x" />
</form>
</div>
</body>
</html>
这里是使用 visual basic 的控制器代码
Imports System.Web.Mvc
Imports Aspentech
Imports Aspentech.HYSYS
Namespace Controllers
Public Class OPenCaseController
Inherits Mvc.Controller
Dim hyCase As SimulationCase
Dim hyApp As Aspentech.HYSYS.Application
Function init() As ActionResult
ViewData("Message") = "Your contact page."
Return View()
End Function
<HttpPost()>
<ActionName("OPEN")>
Public Function init(x) As ActionResult
ViewData("Message") = "Your contact page."
hyApp = CreateObject(“HYSYS.Application”)
hyCase = hyApp.SimulationCases.Open(“E:\otts\st st trial-reem.hsc”)
hyCase.Activate()
Return View()
End Function
End Class
End Namespace
<Button type="button" onclick="location.href='@Url.Action("OPenCaseController", "init")'"> OPENhysys </Button>
问题与@Url.Action()
方法有关,如果右键单击该方法并选择“转到定义”,您可以看到第一个参数应该是动作名称,然后第二个参数是控制器的名称,如下所示(屏幕截图是从 C# 应用程序中捕获的):
有关 @Url.Action()
方法的更多详细信息,请查看 this document。
此外,在MVC应用程序中,即使控制器文件名是“OPenCaseController”,控制器的名称也应该是“OPenCase”。有关 MVC 应用程序的更多详细信息,请查看 this tutorial.
所以,试着把上面的代码改成下面这样:
<Button type="button" onclick="location.href='@Url.Action("init", "OPenCase")'"> OPENhysys </Button>