Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc ASP.NET MVC-表单上的多个按钮_Asp.net Mvc - Fatal编程技术网

Asp.net mvc ASP.NET MVC-表单上的多个按钮

Asp.net mvc ASP.NET MVC-表单上的多个按钮,asp.net-mvc,Asp.net Mvc,我希望在此表单上有多个按钮作为图像: <% Html.BeginForm("Create", "Foos", FormMethod.Post); %> <!-- html form elements --> <%=Html.SubmitImage("Button", "save-button.gif", new { alt = "Save" })%> <% Html.EndForm(); %> 我正在阅读有关Html.Ac

我希望在此表单上有多个按钮作为图像:

<% Html.BeginForm("Create", "Foos", FormMethod.Post); %>

    <!-- html form elements -->
    <%=Html.SubmitImage("Button", "save-button.gif", new { alt = "Save" })%>

<% Html.EndForm(); %>

我正在阅读有关Html.ActionImage的文章,但我在Microsoft.Web.Mvc中没有看到它,我想它已被删除,是否有其他方法添加按钮


我想要一个表单上的保存、删除、发布、取消等按钮,作为图像,最好每个按钮在控制器中调用自己的操作。

您可以使用好的ole html:

<input type="button" name="CancelButton" id="CancelButton" value="Cancel" />

取消
或者其中一个助手

<%= Html.Button("CancelButton", "Cancel", "MyJavascriptFunction()") %>

在任何情况下,您都可能需要编写一点javascript,除非您只想使用一个用于取消的链接


这里有一些关于帮助程序的信息。

当然,一个选择是为每个按钮提供单独的表单。如果没有Javascript或格式错误的HTML,这通常是不可能或不容易的。这很容易让您将每个请求发送到不同的操作方法

这是我为多个图像提交按钮提出的解决方案。我对它不是特别满意,但它可以工作,而且如果在框架中构建了替代方案,以后可以很容易地进行重构

在您的HTML中:

<%= Html.SubmitImage("Dog", "~/images/dog.jpg")%>
<%= Html.SubmitImage("Cat", "~/images/cat.jpg")%>
扩展方法:

<%= Html.SubmitImage("Dog", "~/images/dog.jpg")%>
<%= Html.SubmitImage("Cat", "~/images/cat.jpg")%>
(这将查找名为Remove.x或Skip.x或Add.x的表单参数,并去掉.x部分)

公共静态类FormCollectionExtensions
{
公共静态字符串GetSubmitButtonName(此FormCollection FormCollection)
{
var button=formCollection.Keys.OfType()。其中(x=>x.EndsWith(“.x”)).SingleOrDefault();
//我们有类似AddToCart.x的东西
如果(按钮!=null)
{
返回按钮子字符串(0,按钮长度-2);
}
抛出新的ApplicationException(“未找到图像按钮”);
}
}

注意:另一种方法是使用CSS将背景图像放置在常规Html.SubmitButton按钮类型(常规Html按钮而非Html图像按钮)上,但由于不同浏览器的行为方式不同(),我觉得这一点并不令人满意。

是否希望取消作为文本链接,或者html输入按钮或图像链接?图像就像提交按钮一样。你最后做了什么?这是几个月前的事了谢谢你的回复,但是使用javascript进行导航是不可接受的。你仍然有一些很好的答案,所以+1。
    public ActionResult Index(FormCollection form)
    {
        // get the name of the button that was clicked (sent via either Dog.x or Cat.x since its an image button)
        var buttonName = form.GetSubmitButtonName();

        if (buttonName == "Remove" || buttonName == "Skip")
        {
           Remove(form);
        }
        else if (buttonName == "Add")
        {
           Add(form);
        }
     }
public static class FormCollectionExtensions
{
    public static string GetSubmitButtonName(this FormCollection formCollection)
    {
        var button = formCollection.Keys.OfType<string>().Where(x => x.EndsWith(".x")).SingleOrDefault();

        // we got something like AddToCart.x
        if (button != null)
        {
            return button.Substring(0, button.Length - 2);
        }

        throw new ApplicationException("No image button found");
    }
}