POST method called on MVC UserControls as well as their parent views

后端 未结 2 1210
春和景丽
春和景丽 2020-12-21 08:34

Rewritten: My original post seemed to be misunderstood. I have since reported it as a bug with the following description. My original post for this question

相关标签:
2条回答
  • 2020-12-21 08:54

    EDIT2: I just noticed that you are using RenderAction instead of RenderPartial. I suspect what is happening is that it is using the RequestContext of the posted form to choose which ContactUsForm method to choose when the RenderAction is invoked. This is arguably correct behavior since the action is being invoked from a postback, just not the one you intended. I would approach this in a completely different manner. Have the partial generated by a ViewUserControl and include it on the page using RenderPartial instead. Remove the GET ContactUsForm method and only have the POST version. That is, the form itself is generated as a ViewUserControl with markup predetermined or dynamically generated via parameters passed via ViewData. The form response is handled via the controller action.

    EDIT: Since you indicate that nesting is not the issue, is it possible that you are using javascript (say jQuery) to trigger the submit and your selector is too broad. If, for example, you had code like the following, that would account for the behavior you are seeing.

    $(document).ready( function() {
        $('#mybutton').click( function() {
           $('form').submit();
        });
    });
    

    Original answer: (left for context)

    It sounds like you have nested forms in your view. Try moving the RenderAction outside the form in the parent view and see if that fixes your problem. My feeling about forms in MVC views is that they should be compact and only cover the markup that contains the actual inputs. This is a change from WebForms where you typically wrap all of your mark up within the form. Use CSS to control layout if you need to have the form elements appear to be intermixed.

    0 讨论(0)
  • 2020-12-21 08:54

    This is actually a confirmed bug both in ASP.NET MVC 1.0 with MVC Features library and in ASP.NET MVC 2.0. RenderAction behaves incorrectly when the request is POST. I have submitted the bug in ASP.NET Issue Tracker on Codeplex, please vote for it :) http://aspnet.codeplex.com/WorkItem/View.aspx?WorkItemId=5847

    0 讨论(0)
提交回复
热议问题