在同一视图上提交多个按钮

时间:2022-09-03 16:51:09

I need to have multiple buttons on same view, but for some reason it doesn't work. I did already looked at the same questions but I haven't got a solution.

我需要在同一视图上有多个按钮,但由于某种原因它不起作用。我已经看过同样的问题,但我没有解决方案。

How do I know which button is performed in the view?

我如何知道视图中执行了哪个按钮?

View:

<%@ Page Title="" Language="C#" MasterPageFile="~/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<LIASWeb.Models.Publication>" %>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>NewPublication</h2>

    <% using (Html.BeginForm())
       { %>
    <div>
        <fieldset>
            <p>
                <label for="Name">Naam:</label>
                <%: Html.TextBoxFor(m => Model.nmPublication) %>
            </p>
            <p>
                 <%: Html.TextBox("Search", "type naam")%>
                <input type="submit" name="btnSearch" value="Zoeken" />
            </p>
            <p>
                <input type="submit" name="btnSave" value="Opslaan" />
            </p>
        </fieldset>
    </div>
    <% } %>
</asp:Content>

Controller:

public class PublicationController : Controller
{
    private Repository repository = null;

    public PublicationController()
    {
        repository = new Repository();
    }

    public ActionResult NewPublication(String button)
    {
        if (button == "btnSave")
            // perform save action

            if (button == "btnSearch")
            // perform save action

        return View();
    }

    public ActionResult Index()
    {
        IEnumerable<Publication> model = repository.Publications;
        return View(model);

}

Routings:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Publication", action = "Index", id = UrlParameter.Optional }
        );
}

5 个解决方案

#1


0  

You should use one submit button in one form.

您应该在一个表单中使用一个提交按钮。

Different form for different controller method it's best way IMHO

不同形式的不同控制器方法是最好的方式恕我直言

#2


0  

There are really many solutions for this problem:

这个问题有很多解决方案:

Option 1-

Have something similar to this (I am not checking if the code is correct, so bear with me):

有类似的东西(我不检查代码是否正确,所以请耐心等待):

@using (Html.BeginForm("Controller", "Action1", FormMethod.Post, new { id="MyForm1"}))
{
   <button type="submit" id="btn1">Bt1</button>
}

@using (Html.BeginForm("Controller", "Action2", FormMethod.Post, new { id="MyForm2"}))
{
   <button type="submit" id="btn2">Bt2</button>
}

and now you are pointing to 2 different actions where u can clearly program them, and you can still return them with a View("SameViewForBothForms") or with a redirect to "MainView"

现在你指的是两个不同的动作,你可以清楚地对它们进行编程,你仍然可以使用View(“SameViewForBothForms”)或重定向到“MainView”返回它们。

Option 2- Only one form with 2 buttons > NOT submit type, but simple button, where you will have a Javascript function to CHANGE the value of an hidden field "buttonName" (in the JS function you change this value).

选项2-只有一个带有2个按钮的表单> NOT提交类型,但是简单的按钮,你将有一个Javascript函数来改变隐藏字段“buttonName”的值(在JS函数中你改变这个值)。

Option 3- Any kind of mixes of multiple or single forms are possible....

选项3-任何形式的多种或单一形式的混合物都是可能的....

#3


0  

Try this solution:

试试这个解决方案

public ActionResult NewPublication(string btnSearch)
{
    if (!string.IsNullOrEmpty(btnSearch))
    {
        //btnSearch was clicked
    }
    else
    {
        //btnSave was clicked
    }
}

Check this thread as well

也检查这个帖子

Hope it helps.

希望能帮助到你。

#4


0  

This is something I have done before for wizard-like views and it is possible but I don't think that your approach will work.

这是我之前为类似向导的视图所做的事情,这是可能的,但我不认为你的方法会起作用。

Instead, I suggest trying the solution that I used which was based on this post

相反,我建议尝试使用基于这篇文章的解决方案

You could also just not use form elements for the submission and submit the form using jQuery / Javascript too.

您也可以不使用表单元素进行提交,也可以使用jQuery / Javascript提交表单。

#5


0  

you can identify your button from there name tag like below, You need to check like this in your controller

您可以从下面的名称标签中识别您的按钮,您需要在控制器中进行检查

if (Request.Form["btnSearch"] != null)
{
//Write your code here
}
else if (Request.Form["btnSave"] != null)
{
//Write your code here
}

#1


0  

You should use one submit button in one form.

您应该在一个表单中使用一个提交按钮。

Different form for different controller method it's best way IMHO

不同形式的不同控制器方法是最好的方式恕我直言

#2


0  

There are really many solutions for this problem:

这个问题有很多解决方案:

Option 1-

Have something similar to this (I am not checking if the code is correct, so bear with me):

有类似的东西(我不检查代码是否正确,所以请耐心等待):

@using (Html.BeginForm("Controller", "Action1", FormMethod.Post, new { id="MyForm1"}))
{
   <button type="submit" id="btn1">Bt1</button>
}

@using (Html.BeginForm("Controller", "Action2", FormMethod.Post, new { id="MyForm2"}))
{
   <button type="submit" id="btn2">Bt2</button>
}

and now you are pointing to 2 different actions where u can clearly program them, and you can still return them with a View("SameViewForBothForms") or with a redirect to "MainView"

现在你指的是两个不同的动作,你可以清楚地对它们进行编程,你仍然可以使用View(“SameViewForBothForms”)或重定向到“MainView”返回它们。

Option 2- Only one form with 2 buttons > NOT submit type, but simple button, where you will have a Javascript function to CHANGE the value of an hidden field "buttonName" (in the JS function you change this value).

选项2-只有一个带有2个按钮的表单> NOT提交类型,但是简单的按钮,你将有一个Javascript函数来改变隐藏字段“buttonName”的值(在JS函数中你改变这个值)。

Option 3- Any kind of mixes of multiple or single forms are possible....

选项3-任何形式的多种或单一形式的混合物都是可能的....

#3


0  

Try this solution:

试试这个解决方案

public ActionResult NewPublication(string btnSearch)
{
    if (!string.IsNullOrEmpty(btnSearch))
    {
        //btnSearch was clicked
    }
    else
    {
        //btnSave was clicked
    }
}

Check this thread as well

也检查这个帖子

Hope it helps.

希望能帮助到你。

#4


0  

This is something I have done before for wizard-like views and it is possible but I don't think that your approach will work.

这是我之前为类似向导的视图所做的事情,这是可能的,但我不认为你的方法会起作用。

Instead, I suggest trying the solution that I used which was based on this post

相反,我建议尝试使用基于这篇文章的解决方案

You could also just not use form elements for the submission and submit the form using jQuery / Javascript too.

您也可以不使用表单元素进行提交,也可以使用jQuery / Javascript提交表单。

#5


0  

you can identify your button from there name tag like below, You need to check like this in your controller

您可以从下面的名称标签中识别您的按钮,您需要在控制器中进行检查

if (Request.Form["btnSearch"] != null)
{
//Write your code here
}
else if (Request.Form["btnSave"] != null)
{
//Write your code here
}