0

I use a partial view. In it I have:

@using (Html.BeginForm("MyMethod", "MyController", FormMethod.Post))
{
<input type="submit" id="btn1" name="btnSubmit" value="Add record" />
<input type="submit" id="btn2" name="btnSubmit" value="Use record" />
}

Then in the Controller [HttpPost] method I have:

[HttpPost]
public ActionResult MyMethod(string btnSubmit)
{
...
}

The problem is the btnSubmit is always null. I tried calling this partial view directly and it returns the right value.

Any idea how to fix this? Thanks in advance.

Miss
  • 13
  • 6
  • What does the HTML produced look like? – GraemeMiller Sep 19 '13 at 23:32
  • http://stackoverflow.com/questions/547821/two-submit-buttons-in-one-form – Jeremy Bell Sep 20 '13 at 02:08
  • I have finally figured it out. It's a dumb mistake, on form submit event, I did return false which cancelled the submit event and the btnSubmit value wasn't passed because of that. Thanks to everyone who has helped. – Miss Sep 21 '13 at 00:42

2 Answers2

0

I tried executing your code, it works fine either way i.e. calling the Partial View directly or calling it in another view.

Can't recognize what is the issue in your source. I guess you must provide some more information on this.

Here's what i've tried in my solution. You can check if its helpful o you

Partial View:

@using (Html.BeginForm("MyMethod", "Home", FormMethod.Post))
{
<input type="submit" id="btn1" name="btnSubmit" value="Add record" />
<input type="submit" id="btn2" name="btnSubmit" value="Use record" />
}

Controller:

        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";

            return View();
        }

        public ActionResult MyMethod()
        {
            return PartialView();
        }

        [HttpPost]
        public ActionResult MyMethod(string btnSubmit)
        {
            return RedirectToAction("Index");
        }

and Added the following line in Index View:

@Html.Action("MyMethod")
Shahbaz Chishty
  • 442
  • 1
  • 3
  • 9
  • Thanks for your comment. In my controller action i do have: if (Request.IsAjaxRequest()) { return PartialView("_MyView", vm); } return View(vm); If I call this controller directly without using ajax from another controller's view so it bypass the if statement, in the [HttpPost] method, I get the btnSubmit value no problem. Is the problem the ajax request? – Miss Sep 20 '13 at 19:15
  • I tried calling the partial view with both the methods, i.e. jQuery and Ajax also., but its still working fine with me. I've used $("#div").load("/../Home/MyMethod", function () { }); in jQuery and $.ajax({ type: "GET", url: "/../Home/MyMethod", success: function (data) { $("#div").html(data); } }); for Ajax – Shahbaz Chishty Sep 21 '13 at 08:06
  • I think there some other issue with your code. You can try checking your calling method again. – Shahbaz Chishty Sep 21 '13 at 08:09
0

You are thinking "ASP.NET Web Forms" way, where you have events with information on the element which triggered them In MVC there is no such concept.

In MVC you need to have and invoke different actions (controller methods) for anything you want to do. This actions can have parameters. If this parameters have the right name, they will receive the posted data. In your case, you're expecting that btnSubmit "automagically" gets the name of the button, but this isn't posted in any way.

You can solve this in 3 different ways:

  • create a hidden field in your form, and, when the submit button is clicked (onclick button event) populate it with the needed information. For this to work in your sample, the hidden field should have the btnSubmit name.

  • create two different actions, and make your button post the form to different actions. You can use UrlHelper to get the url of the action to post. (Url.Action method).

  • change the form's action attribute in the submit button onclick

The second solution involves one of these 2 techniques:

  • formaction attribute which will only work in modern browsers
  • simulate the formaction attribute using unobtrusive javascript and a "data-formaction" attribute
JotaBe
  • 34,736
  • 7
  • 85
  • 109