简体   繁体   中英

ASP.Net MVC 4 formcollection issue

I am having an odd issue with the ASP.Net MVC 4 formcollection for a submitted form. Here are the facts of the situation:

  1. ASP.Net MVC 4 project
  2. I have a form on a view with multiple inputs.
  3. I have two submit buttons on the form, each with a different name and each mapped to a different action in the view controller (using HttpParamActionAttribute).
  4. I have two Actionlink buttons outside of the form with onclick events that point to each of the two submit buttons inside the form. In other words, when each of these buttons is clicked, it generates a click of the corresponding submit button within the form.
  5. The issue I am having is that the FormCollection of the submitted form in the controller is not correct if the actionlink button is pressed. If the form button is pressed directly, then all the correct form values are seen in the formcollection. However, if the actionlink button is pressed, then the formcollection does not show the values of the submitted form. Instead it shows the old (default) values of the form.

Why is there is a difference?

I thought that if the actionlink button simulated a click of the form submit then it would do the very same thing as if the form submit had been pressed itself but apparently that is not so. Why Not?

Try something like this:

$('#outsideFormbutton').on('click', function()
{
   // This will call your form submit button
   $('#formButton').click();
});

$('#formButton').on('click', function(e)
{
   // Prevents the default action of the form button
   e.preventDefault();
   // Manually submit your form
   $('#myForm').submit();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM