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.

3
  • What does the HTML produced look like? Commented Sep 19, 2013 at 23:32
  • stackoverflow.com/questions/547821/… Commented Sep 20, 2013 at 2: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. Commented Sep 21, 2013 at 0:42

2 Answers 2

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")
Sign up to request clarification or add additional context in comments.

3 Comments

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?
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
I think there some other issue with your code. You can try checking your calling method again.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.