POST方法要求用户控件的MVC,以及他们父母的意见 [英] POST method called on MVC UserControls as well as their parent views

查看:102
本文介绍了POST方法要求用户控件的MVC,以及他们父母的意见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

改写:我原来的职位似乎被误解。因为我已经报告说,它与下面的描述中的错误。 HR >;我原来的职位对这个问题可以在下面的第二个&LT找到。


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 can be found below the second <HR>.


我有一个用户控件与邮政的重大课题。

I have a major issue with POST in a user control.


  • 我已经有包含两个ActionMethods称为'的ContactForm'控制器的用户控件。第二个有ActionVerb.POST它向后回响应。用户控制是通过AJAX主要用于 - 但实际上在这里无关紧要。显然,这些行动方法呈现的局部视图。

  • I have a UserControl which has a controller containing two ActionMethods called 'ContactForm'. The second has ActionVerb.POST on it to respond to the post-back. The user control is primarily used via AJAX - but that actually is irrelevant here. Obviously these action methods render a partial view.

我有包含EnterContest额外的HTML形式的主页。再次 - 其控制器有两个ActionMethods称为'EnterContest',其中之一响应ActionVerb.POST。此视图包含在侧栏中的的ContactForm'的呈现有:

I have a main page containing an additional html form for 'EnterContest'. Again - its controller has two ActionMethods called 'EnterContest', one of which responds to ActionVerb.POST. This view contains the 'ContactForm' in a side bar which is rendered with :

&LT;%Html.RenderAction(ContactUsForm,公司,新{commentsBoxHeader =问题}); %GT;

回发'EnterContest'表(视图上的主要形式)时出现问题。

The problem occurs when posting back the 'EnterContest' Form (the main form on the view).

从菲德勒POST请求只包含(显然不包含任何从接触POST数据的形成我们因为多数民众赞成完全独立的HTTP ACTION)这个查询字符串。

The POST request from Fiddler contains just this query string (obviously not containing any of the POST data from the contact us form because thats a completely separate HTTP ACTION).

contestId = ND09和放大器;电子邮件=弗雷德&安培; btnEnterContest =提交

contestId=ND09&email=fred&btnEnterContest=Submit

(是的,这看起来像一个GET但多数民众赞成
  什么菲德勒显示了一个POST太)

(yes, this looks like a GET but thats what Fiddler shows for a POST too)


  • 第一 - (预期) - 的'EnterContest(的FormCollection数据)方法被调用的主控制器。这种处理表单提交给参加比赛了 - 调用web服务等。

  • First - (as expected) - the 'EnterContest(FormCollection data) method is called in the main controller. This processes the form submission to enter the contest - calls the webservice etc..

    二 - (预计不会) - 在的ContactForm'控制器的POST方法被调用。这立即崩溃,因为它缺少预期参数​​ - 我们不希望它称为反正。这种情况同一个Http请求过程中。

    Second - (NOT expected) - The POST method of the 'ContactForm' controller is called. This immediately crashes because it is missing expected parameters - and we don't want it called anyway. This happens during the same Http request.

    如果我看一看堆栈跟踪 - 它正在从动态生成aspx页面被称为 - 源自上述code的Html.RenderAction线。所以,很显然正在发生的事情是code,它正在部分地渲染联系我们的操作方法查看请求,并认为有处理POST所以它路由有一个方法 - 这是非常糟糕的。它可能介于在周围的框架此方法:

    If I take a look at the stack trace - it is being called from the dynamically generated aspx page - originating at the Html.RenderAction line of code shown above. So obviously what is happening is the code that is looking to partially render the 'ContactUs' action method looks at the Request and sees there is a method to handle POST so it routes it there - which is VERY BAD. Its probably somewhere around this method in the framework :

    System.Web.Mvc.dll!System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod
    

    此行​​为是非常混乱,真正打破了似乎是一个简单的页面。我是pretty相信这是一个错误 - 因为我看不到一个优雅的工作,周围没有在我的控制器一些真正笨拙的检查。我想是的RenderAction期货 - 但我不知道,如果问题是present存在或在主框架

    This behavior is VERY confusing and really breaks what seemed to be a simple page. I'm pretty sure it is a bug - because I cannot see an elegant work around without some really clumsy checking in my controller. I think RenderAction is in futures - but I don't know if the issue is present there or in the main framework.

    我想弄清楚究竟是不是发生了什么:

    Just to clarify what is NOT happening :


    • 任何聪明的jQuery

    • 多个HTTP请求(小提琴手验证)

    • 嵌套形式

    感谢


    原帖

    我使用的是的RenderAction HTML在ASP.NET MVC扩展。

    I am using the RenderAction Html extension in ASP.NET MVC.

    我遇到了意想不到的事情,但正在更为合理,因为我想它。

    I came across something unexpected, but which is making more sense as I think about it.

    可以说我有一个包含'的RenderAction'方法生成的页面的一部分接触的视图。

    Lets say I have a view containing a 'RenderAction' method to generate the contact for a part of the page.

    <% Html.RenderAction("ContactUsForm", "Company", 
       new { commentsBoxHeader = "Questions" }); %>
    

    在这种情况下,它产生的局部视图创建一个AJAX的形式,通过&LT帖子后面;%= Html.BeginAjaxForm()%方式&gt;

    In this case the partial view it generates creates an ajax form which posts back via <%= Html.BeginAjaxForm() %>.

    所以我当然需要一个ActionResult处理的AJAX回发。

    So of course I need an actionresult to handle the AJAX postback.

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult ContactUsForm(FormCollection formdata)
    

    当包含此部分动作呈现在父视图有一个正常(非Ajax)的形式与POST操作

    现在出现问题。什么情况是,对于 ContactUsForm 操作POST方法是除了对主视图的POST操作调用。这里面的行动 FORMDATA 属性包含父视图所有属性 - 这样 ContactUsForm 与空引用或某事死这样的。

    Now the problem occurs when the parent view containing this partial render action has a normal (non-ajax) form with a POST action. What happens is that the POST method for the ContactUsForm action is call in addition to the POST action for the main view. Inside this action the formdata property contains all the properties for the parent view - so ContactUsForm dies with a null reference or something like that.

    我来了3种可能的解决方案:

    I've come up with 3 possible solutions :

    1)为后回网页上的任何用户控件创建不同的操作名称。这样做,你有比创建的局部视图回发到不同的功能的缺点。通常,这可能是比较繁琐,但是这是我在做什么现在。

    1) create a different action name for the post back for any user controls on the page. This has the disadvantage that you have to post back to a different function than created the partial view. Often this can be more cumbersome but this is what i'm doing right now.

    2)检查每个POST方法(你必须记住运行在每个用户控件的POST操作方法,这个检查),看看是否表单数据是用于这种形式它,如果不返回默认视图。

    2) check in every POST method (you'd have to remember to run this check in every user control's POST action method) to see if the form data is intended for that form it, and if not return a default view.

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResultContactUsForm(FormCollection formData)
        {
            if (formData["btnSubmitContactUsForm"] == null) {
    
                // "This form is not for us!";
                // figure out how (if is possible) to return the get default view here
                // and call it with the right arguments
            }
        }
    

    3)报告这是一个错误。

    3) report it as a bug.

    我应该在这里做什么?我倾向于这种思想作为一个bug

    What should I be doing here? I'm leaning towards thinking of this as a bug

    编辑:我要强调更多的一个非常重要的事情是,无论是POST方法被称为 - 所以它不只是像somethig嵌套形式。

    One very important thing I need to stress more is that BOTH POST methods are called - so its not just somethig like a nested form.

    编辑2:在提琴手我看只有一个请求。问题是,当它试图使 ContactUsForm 在已经处理了我的主网页POST它击中'POST'法'ContactUsForm'代替非员额后,处理程序。

    Edit 2: In Fiddler I see only one request. The problem is when it tries to render the ContactUsForm after having already handled the POST for my main page it hits the 'POST' method for 'ContactUsForm' instead of the non-post handler.

    推荐答案

    EDIT2 :我刚才注意到您正在使用的的RenderAction代替的RenderPartial。我怀疑所发生的事情是,它是用贴形式RequestContext的选择调用时的RenderAction选择哪种方法ContactUsForm。这无疑是正确的行为,因为行为的从回传被调用,只要你不打算的人。我会以完全不同的方式接近这一点。具有由ViewUserControl产生的部分和它包括使用的RenderPartial代替在页面上。删除GET ContactUsForm方法只有POST版本。即,形式本身作为通过经由ViewData的传递的参数pdetermined或动态产生的标记$ P $一个ViewUserControl产生。形式响应通过控制器操作处理。

    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.

    修改:既然你指出嵌套不是问题,是你在使用JavaScript(jQuery的说)来触发,并提交你的选择范围太广没有可能。如果,例如,你有code像下面这样,这将占到您所看到的行为。

    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();
        });
    });
    

    原来的答复:(左上下文)

    这听起来像你嵌套在视图的形式。尝试移动形式之外在的RenderAction父视图,看看是否能解决您的问题。我的关于MVC意见形式的感觉是,他们应该是紧凑,只包括包含实际投入的标记。这是WebForms的,你通常包装所有你的商标达形式中的变化。使用CSS来控制布局,如果你需要具备的元素似乎是混合的形式。

    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.

    这篇关于POST方法要求用户控件的MVC,以及他们父母的意见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆