使用“action ='/ action'”提交表单数据,并在控制器中关联该操作的方法 - ASP.NET

时间:2022-11-23 11:36:46

I always worked with J2EE plateform and now I should develop an application using ASP.NET technology. I know that we can send form data using different actions in J2EE. Then we can associate a method to this action in the controller.

我一直使用J2EE平台,现在我应该使用ASP.NET技术开发一个应用程序。我知道我们可以使用J2EE中的不同操作发送表单数据。然后我们可以将方法与控制器中的此操作相关联。

Example;

例;

// html form
<form id="postToURL" action="/GenerateExcel">
       // Some form data to send
</form>

-------------------------------------------------------
@RequestMapping(value = "/GenerateExcel", method = RequestMethod.POST)
public ModelAndView someMethod(){
    // some code
}

My question is how can I do the same in an ASP.NET application with a method like this;

我的问题是如何在ASP.NET应用程序中使用这样的方法执行相同的操作;

 /* .NET Annotations */
 public virtual ActionResult GenerateExcel(string data)
 {
    // Some Code.....
    // System.Diagnostics.Debug.WriteLine("CSVTO EXCEL ACtion intercepted");
    // .................
 }

Thanks in advance for your help.

在此先感谢您的帮助。

1 个解决方案

#1


0  

You could just use the following if it's same controller scope:

如果它是相同的控制器范围,您可以使用以下内容:

<form method="post" action="<%= Url.Action("GenerateExcel") %>" id="postToURL">
    // Some form data to send
</form>

Otherwise you have to define the controller of the action:

否则,您必须定义操作的控制器:

<form method="post" action="<%= Url.Action("GenerateExcel", "ControllerName") %>" id="postToURL">
    // Some form data to send
</form>

Furthermore you can define, that your action only accepts POST requests like this:

此外,您可以定义,您的操作只接受这样的POST请求:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult GenerateExcel(string data)
{
  // Some Code.....
  // System.Diagnostics.Debug.WriteLine("CSVTO EXCEL ACtion intercepted");
}

You probably won't need the virtual keyword here, if you don't want to override the method in a derived class.

如果您不想覆盖派生类中的方法,则可能不需要此处的virtual关键字。

#1


0  

You could just use the following if it's same controller scope:

如果它是相同的控制器范围,您可以使用以下内容:

<form method="post" action="<%= Url.Action("GenerateExcel") %>" id="postToURL">
    // Some form data to send
</form>

Otherwise you have to define the controller of the action:

否则,您必须定义操作的控制器:

<form method="post" action="<%= Url.Action("GenerateExcel", "ControllerName") %>" id="postToURL">
    // Some form data to send
</form>

Furthermore you can define, that your action only accepts POST requests like this:

此外,您可以定义,您的操作只接受这样的POST请求:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult GenerateExcel(string data)
{
  // Some Code.....
  // System.Diagnostics.Debug.WriteLine("CSVTO EXCEL ACtion intercepted");
}

You probably won't need the virtual keyword here, if you don't want to override the method in a derived class.

如果您不想覆盖派生类中的方法,则可能不需要此处的virtual关键字。