在一种形式ASP.NET MVC 3多输入提交 [英] ASP.NET MVC 3 Multiple Submit Inputs in One Form

查看:108
本文介绍了在一种形式ASP.NET MVC 3多输入提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有多个操作按钮以相同的形式是一个问题。

I am currently having an issue with multiple action buttons being in the same form.

第一个按钮将执行验证,而第二个按钮,将节省的个人资料。第三个将简单的把用户重定向出的页面,但他们仍然需要通过控制器中的一些跟踪的目的。最后一个按钮删除。因为他们放在一起,我也需要通过ModelBinding POST过去了,这是不可能将它们分成多个表单。

The first button would perform verification while the second button would save profile. The third would simple redirect the user out of the page, but they are still required to go through controller some tracking purposes. Last button is delete. Because they are placed together and I do need ModelBinding passed through POST, it's impossible to separate them into multiple forms.

目前,为了区分被单击的动作,我有我的形式隐藏输入的onclick,javascript中会更新隐藏输入,所以它会被传递回控制器。

Currently, in order to differentiate which action is being clicked, I have a hidden input in my form and onclick, javascript would update the hidden input so that it will be passed back to the controller.

我这样做的原因是因为一些奇怪的原因,的FormCollection并不想牵我的提交值。我试图通过

The reason I did this was because for some weird reasons, FormCollection doesn't want to hold my submit values. I tried accessing buttons in controller via

formCollection["verify"]

但它原来是空。输入的两个编号和名称提交设置验证。

But it turns out to be null. Both id and name of the input submit is set to verify.

我也尝试了很多其他建议像<一个href=\"http://weblogs.asp.net/dfindley/archive/2009/05/31/asp-net-mvc-multiple-buttons-in-the-same-form.aspx\">this和<一个href=\"http://blog.ashmind.com/2010/03/15/multiple-submit-buttons-with-asp-net-mvc-final-solution/\">this但无济于事。是否有我的问题,更好的方法,而无需使用JavaScript来改变隐藏的投入?

I also tried a lot of other suggestions like this and this but to no avail. Is there a better approach to my problem without using javascript to alter hidden inputs?

推荐答案

最好的办法是有单独的操作处理不同的呼叫按钮,在<一个解释href=\"http://blog.ashmind.com/2010/03/15/multiple-submit-buttons-with-asp-net-mvc-final-solution/\">this文章。

The best approach is to have separate actions handling the different button calls as explained in this article.

如果你想有一个丑陋的动作做的所有的东西,那么你可以给你的提交按钮的名称:

If you want to have one ugly action doing all the stuff then you could give your submit buttons names:

@using (Html.BeginForm())
{
    ... input fields for the model

    <button type="submit" name="btn" value="verify">Verify data</button>
    <button type="submit" name="btn" value="save">Save data</button>    
    <button type="submit" name="btn" value="redirect">Redirect</button>
}

您不需要任何隐藏字段或javascript。然后在你的控制器动作,你会检查 BTN 参数的值(这显然将是您观看模型部分):

You don't need any hidden fields or javascript. And then in your controller action you would check for the value of the btn parameter (which obviously will be part of you view model):

[HttpPost]
public ActionResult Foo(MyViewsModel model)
{
    if (model.Btn == "verify")
    {
        // the Verify button was clicked
    }
    else if (model.Btn == "save")
    {
        // the Save button was clicked
    } 
    else if (model.Btn == "redirect")
    {
        // the Redirect button was clicked
    } 
    else
    {
        // ??? throw
    }

    ...
}

当然,如果你按照我的建议,并分离你的动作(如文章中概述):

Of course if you follow my advice and separate your actions (as outlined in the article):

@using (Html.BeginForm("Action", "Home"))
{
    ... input fields for the model

    <input type="submit" name="verify" value="Verify data" />
    <input type="submit" name="save" value="Save data" />
    <input type="submit" name="redirect" value="Redirect" />
}

和则:

[HttpParamAction]
[HttpPost]
public ActionResult Verify(MyViewModel model)
{
    ...
}

[HttpParamAction]
[HttpPost]
public ActionResult Save(MyViewModel model)
{
    ...
}

[HttpParamAction]
[HttpPost]
public ActionResult Redirect(MyViewModel model)
{
    ...
}

这是一个更干净code不违反单一职责原则。

which is a far cleaner code which doesn't violate the Single Responsibility Principle.

这篇关于在一种形式ASP.NET MVC 3多输入提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆