我怎样才能改变一个表单提交的基础上被点击ASP.NET MVC什么按钮的动作? [英] How can I change the action a form submits to based on what button is clicked in ASP.NET MVC?

查看:252
本文介绍了我怎样才能改变一个表单提交的基础上被点击ASP.NET MVC什么按钮的动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似如下实体模型(从Facebook Photoshop处理)为我们的信息在我们的ASP.NET MVC网站查看窗体。

I have a form that looks like the following mock-up (photoshopped from Facebook) for our Messages view on our ASP.NET MVC site.

我期待获得标记为未读和删除功能的,看起来像这样的形式合作。我试图找出如何我可以用HTML表单做到这一点。

I'm looking to get the "Mark as Unread" and "Delete" functionality working for a form that looks like this. I'm trying to figure out how I can do this with html forms.

我如何击中标记为未读向一种形式的网址,而删除提交相同的形式,以一个不同的URL?

How do I make hitting the "Mark as Unread" submit to one form url, and "Delete" submit the same form to a different url?

如果你点击标记为未读,我想它提交表单,以/信息/ MarkUnread,如果它是删除按钮,我想它提交表单,以/消息/删除。

If you hit "Mark as Unread", I'd like it to submit the form to "/Messages/MarkUnread" and if it is the "Delete" button, I'd like it to submit the form to "/Messages/Delete".

我希望得到这个工作没有JavaScript和它一旦我将添加了jQuery插件的形式,因此会做,如果启用了JS ajax的提交,否则将做一个正常的表单提交。

I'd like to get this working without javascript, and once it is I'll add the jQuery forms plugin so it will do an ajax submit if JS is enabled, otherwise it will do a normal form post.

推荐答案

我是一个总的MVC福利局,但我相信表单只能提交一个动作。

I am a total MVC newb, but I believe the form can only submit to one action.

这是说,你要发布帖子的行动,我知道你将能够告诉哪个按钮被点击并适当地处理事情,例如:

That said, in the action you are POSTing to, I know you will be able to tell which button was clicked and handle things appropriately, e.g.:

    [AcceptVerbs(HttpVerbs.Post)]
    public ViewResult Index(string btnCompose, string btnMarkAsRead, string btnDelete)
    {
        if (btnCompose != null)
        {
            // Compose was clicked, handle appropriately
        }
        else if (btnMarkAsRead != null)
        {
            // Mark As Read was clicked, handle appropriately
        }
        else if (btnDelete != null)
        {
            // Delete was clicked, handle appropriately
        }
    }

这假定您的HTML元素被命名为btnDelete等。

This assumes that your HTML elements are named "btnDelete", etc.

这篇关于我怎样才能改变一个表单提交的基础上被点击ASP.NET MVC什么按钮的动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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