简体   繁体   English

ASP.net MVC可以重载Index()HttpPost吗?

[英]ASP.net MVC Can you overload an Index() HttpPost?

Let's say I have a page with 2 forms that both submit different data on the same page, one submits two ID's, and the other submits only 1 ID. 假设我有一个包含2个表单的页面,它们都在同一页面上提交不同的数据,一个提交两个ID,而另一个仅提交1个ID。 They are both posting back to the same page (itself). 它们都发回到同一页面(本身)。 Here is what the HTML would look like... 这是HTML的样子...

<form method="post">

<select name="regID">
...
</select>

<select name="jobID">
...
</select>

<input type="submit" value="Add">

</form>

<form method="post">

<button name="ID" type="submit" value="@ID">Remove</button>    

</form>

Now, to handle the first form in the controller I can do 现在,要处理控制器中的第一个表格,我可以做

 [HttpPost]
 public ActionResult Index(int regID, int jobID)
 {
 ....
 }

However, if I try to handle the second form by adding 但是,如果我尝试通过添加来处理第二种形式

 [HttpPost]
 public ActionResult Index(int ID)
 {
 ....
 }

When I click the submit button, I will now get the error 当我单击提交按钮时,我现在会收到错误消息

The current request for action 'Index' on controller type 'UserJobController' is ambiguous between the following action methods:
System.Web.Mvc.ActionResult Index(Int32) on type careerninja.Controllers.UserJobController
System.Web.Mvc.ActionResult Index(Int32, Int32) on type careerninja.Controllers.UserJobController 

So, is it possible in the controller to overload the [HttpPost] method with different values to handle 2 different sets of form data, or is this not possible? 因此,是否有可能在控制器中用不同的值重载[HttpPost]方法以处理2套不同的表单数据,或者这不可能吗? Is there another solution I might not be grasping to handle this sort of issue? 我可能还没有其他解决方案来解决此类问题?

Basically, for the second form I want to have a "Remove" button that when clicked, calls the controller to remove the item, removes the item, then returns the Index() view. 基本上,对于第二种形式,我希望有一个“删除”按钮,单击该按钮时,将调用控制器删除该项目,然后删除该项目,然后返回Index()视图。

I think an improvement in your design would make the problem you are having not an issue anymore. 我认为设计上的改进将使您不再遇到问题。 It sounds like you believe everything might have to go through your Index() method, which is not the case. 听起来您似乎认为一切都必须通过Index()方法,但事实并非如此。 Redesigning your method's name to the behavior of what the action is doing is typically how I name my methods. 将方法的名称重新设计为操作所执行的行为通常就是我为方法命名的方式。

Basically, for the second form I want to have a "Remove" button that when clicked, calls the controller to remove the item, removes the item, then returns the Index() view. 基本上,对于第二种形式,我希望有一个“删除”按钮,单击该按钮时,将调用控制器删除该项目,然后删除该项目,然后返回Index()视图。

So create your method called Remove() and have it redirect to the Index() 因此,创建名为Remove()方法,并将其重定向到Index()

public ActionResult Remove(int id)
{
  // do some work 

  this.RedirectToAction("Index");
}

I would recommend making your method names represent what they are doing. 我建议使您的方法名称代表它们在做什么。

public ActionResult Add(int regID, int jobID)
{
  // do some work

  this.RedirectToAction("Index");
}

Note : This is also an important design for the User Interface. 注意 :这也是用户界面的重要设计。 When a page typically does a POST to the server, and then the server returns HTML, if the users decides to refresh the page , a popup will typically be presented asking them if they want to resubmit data. 当页面通常对服务器执行POST,然后服务器返回HTML时,如果用户决定刷新页面,通常会显示一个弹出窗口,询问他们是否要重新提交数据。 Instead the previous examples did a server side redirect, which starts a second request as a GET and prevents the popup from occurring on refresh. 相反,前面的示例进行了服务器端重定向,服务器端重定向以GET身份启动了第二个请求,并防止在刷新时发生弹出窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM