0

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 }
        );
}
nemesv
  • 138,284
  • 16
  • 416
  • 359
Tako
  • 1
  • 1
  • 1
  • possible duplicate of [How do you handle multiple submit buttons in ASP.NET MVC Framework?](http://stackoverflow.com/questions/442704/how-do-you-handle-multiple-submit-buttons-in-asp-net-mvc-framework) – nemesv Jan 10 '13 at 10:29
  • This question is more than 4 days old, are you sure that any of the present answers is not the "correct one"? If yes, then chose it as the correct answer, if not, then please give us more details on your problem. Thanks. – Dryadwoods Jan 14 '13 at 15:52

5 Answers5

0

You should use one submit button in one form.

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

Victor Leontyev
  • 8,488
  • 2
  • 16
  • 36
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.

MarkG
  • 1,859
  • 1
  • 19
  • 21
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"

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).

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

Dryadwoods
  • 2,875
  • 5
  • 42
  • 72
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.

Community
  • 1
  • 1
Karthik Chintala
  • 5,465
  • 5
  • 30
  • 60
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
}
Kinjal Gohil
  • 958
  • 9
  • 15