是否控制器的HttpPost ActionResult的获取过程中所谓的在,启动或表单提交事件之后? [英] Does the Controller's HttpPost ActionResult get called at the start of, during, or after the form's submit event?

查看:222
本文介绍了是否控制器的HttpPost ActionResult的获取过程中所谓的在,启动或表单提交事件之后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个后续在这里我的问题:<一href=\"http://stackoverflow.com/questions/18346600/why-are-my-replacement-parameters-getting-transformed-into-empty-strings\">Why是我的&QUOT;更换参数&QUOT;得到转化为空字符串?

好吧,也许有人可以帮助我了解了什么是怎么回事(请注意,我没有写这个code,所以我从刚刚直升机投进一个问题熊角度接近它地形不熟)。

,因为它似乎更自然/常规的这样做,我改变了我的处理程序从一个提交按钮单击处理程序表单的提交处理程序:

  $(形式)。递交(函数(){

这是导致整个过程失败是他们被解析为空字符串(GUID,的SerialNumber,并REPORTNAME)在相应型号的基类中声明的模型值:

 公共类ReportModelCore
{
    公共字符串GUID {搞定;组; }
    公共字符串的SerialNumber {搞定;组; }
    公共字符串REPORTNAME {搞定;组; }
    公共BOOL DataSourceSuccess {搞定;组; }
    公共字符串DataSourceMessage {搞定;组; }
    公共字符串消息{搞定;组; }
}

...而这些核心成员在控制器有条件赋值:

  model.SerialNumber = Serial.ToString();
model.ReportName =ReceiptCollection;DatasourceCreatorResultContract结果= Operations.CreateDataSource(标准);如果(result.type == ResultType.Success)
{
    model.GUID = result.GUID;
    。 。 。
}
否则,如果(result.type == ResultType.SuccessWithAlert)
{
    model.GUID = result.GUID;
    。 。 。
}
否则//失败&LT;没有在diesem秋季分配GUID
{
    。 。 。

...但我不神交他们是如何预计在表单的提交处理程序,在那里他们被引用值:

 。 。 。
    如果(resultsText ==0){
        $(#NumberOfResults),CSS(色,红)。
    }其他{
        VAR HREF ='/@ConfigurationManager.AppSettings[\"ThisApp\"]/TLDCriteria/LoadReport';
        // report_parms(原文如此)从LoadReport引用
        VAR report_parms = {
            GUID:@ Model.GUID,
            的SerialNumber:@ Model.SerialNumber,
            REPORTNAME:@ Model.ReportName
        };
        window.open(HREFreport_window,可调整大小= 1,宽度= 850,左=+(screen.width / 2 - 425));
    }
。 。 。

IOW:什么时候控制器的[HttpPost]的ActionResult被解雇?表格前提交事件​​完成?如果是这样,当是什么呢?

如果不是,这是为什么code引用它不可能被分配值的那些模型会员,但(当它们是空字符串,因为它们目前是,它会导致整个套件和kaboodle以隆重,但可耻失败)?

更新

* report_parms *是,作为注释的注释,从LoadReport,这是一个Silverlight出没文件与引用

 &LT;脚本类型=文/ JavaScript的&GT;
    功能get_user_name(){
        返回@ User.Identity.Name
    }    功能get_xml_data(){
        返回window.opener.xml_data;
    }    功能get_receipt_parms(){
        返回window.opener.receipt_parms;
    }    功能get_report_parms(){
        返回window.opener.report_parms;
    }
&LT; / SCRIPT&GT;

通过它在Firefox>脚本步进,它的确显示report_parms为空字符串,这三个成员的值;我并不感到惊讶,但同样:有一个原因,有人会的期望的这些丘壑要在这一点上填充 - 做他们需要?如果是这样,怎么样?

更新2

MisterJames,

是的,有一个:

 返回查看(模型);

...在[HttpPost]的ActionResult的结束,但事实证明,这是从来没有达到;显然,另一个问题是preventing的发生;实际上可以,好像样的先有鸡还是先有蛋的问题:我在想,这code的原因:

  DatasourceCreatorResultContract结果= Operations.CreateDataSource(标准);

...是失败是因为Criteria对象传递给的createDataSource()是不完全正确,这是因为这些report_parms是空的;但也可能是,OTOH,问题出在其他地方。即:将永远达不到线,因为code失败,如果(result.type == ResultType.Success)的由于缺乏正确的身份验证,显然

我认为这是我report_parms问题引起的,但也许我的report_parms问题是由这个引起的!

code的整个部分是:

 。 。 。
    model.SerialNumber = Serial.ToString();
    model.ReportName =ReceiptCollection;    DatasourceCreatorResultContract结果= Operations.CreateDataSource(标准);    如果(result.type == ResultType.Success)//&LT; - 这没有达到; conn.Open();在另一个类中发生故障也可以是之前
    {
        model.GUID = result.GUID;
        model.DataSourceSuccess = TRUE;
        model.NumberOfResults = result.Count;
        model.message =的String.Format({0}结果,result.Count);
    }
    否则,如果(result.type == ResultType.SuccessWithAlert)
    {
        model.GUID = result.GUID;
        model.DataSourceSuccess = TRUE;
        model.NumberOfResults = result.Count;
        model.message = result.Message;
    }
    否则//失败
    {
        model.DataSourceMessage = result.Message;
        model.DataSourceSuccess = FALSE;
        model.NumberOfResults = 0;
        model.message = result.Message;
    }
}
返回查看(模型);


解决方案

粘土,你需要确保填充控制器实际上传递值到视图模型后。你应该看到类似这样的:

 返回查看(模型);

您视图输入该模型(这是我从您的其他问题看到的),因此,当您在模型视图方法传递,你实际上指示的框架,因为它建立与数据集工作风景。

我同意一些,这似乎不可思议,但请记住,这些只是类和方法。的的控制器从具有调用它的查看方法的基本控制器继承。你在做什么是传递一个对象来表示方法,这反过来又注入了进入的Razor视图引擎生成的结果 - 即呈现并返回给浏览器的HTML页面

要你的问题,提交表单(这发生在客户端上)和ActionResult的(这恰好在服务器上)的调用都只能凭借Web浏览器发出请求,注重结果的连接。形式是完全提交的浏览器的请求的一部分。该方法不会被调用,直到服务器接收到完整的请求(并创建控制器的一个实例,等等)。

您典型Asp.Net MVC流程可能是这样的:


  1. 在ActionResult的方法浏览器请求的结果被调用。作为一种形式,你有可能加载了一个视图模型,并把它传递给视图。

  2. 视图由的Razor视图引擎处理。传递在该模型将被提供给引擎和整个视图中使用。

  3. 浏览器(ERM ......用户)提交表单。这些值放入请求对象和形状codeD。

  4. 的MVC框架将再次加速,创建了一个控制器,采用模型结合的形式恩codeD值推入你的参数 - 如果可以 - 并调用你的方法HttpPost

如果您没有在步骤2中看到的视图(HTML结果页面)的值,最好设置在步骤1中断点在那里你可以评估的为什么的模型是不是被稀少。

希望这有助于一些。如果你想在管道的更精细的看法,你可以看看这个帖子:的 Asp.Net MVC管道

干杯。

This is a followup to my question here: Why are my "replacement parameters" getting transformed into empty strings?

Okay, maybe somebody can help me understand what's going on here (note that I didn't write this code, so I'm approaching it from the perspective of a problem bear that has just been heli-dropped into unfamiliar terrain).

As it seemed more natural/conventional to do so, I changed my handler from a submit button click handler to the form's submit handler:

$("form").submit(function () {

The model values that are causing the whole process to fail being they are being resolved to empty strings (GUID, SerialNumber, and ReportName) are declared in the corresponding model's base class:

public class ReportModelCore
{
    public string GUID { get; set; }
    public string SerialNumber { get; set; }
    public string ReportName { get; set; }
    public bool DataSourceSuccess { get; set; }
    public string DataSourceMessage { get; set; }
    public string message { get; set; }
}

...and these core members are conditionally assigned values in the Controller:

model.SerialNumber = Serial.ToString();
model.ReportName = "ReceiptCollection";

DatasourceCreatorResultContract result = Operations.CreateDataSource(criteria);

if (result.type == ResultType.Success)
{
    model.GUID = result.GUID;
    . . .
}
else if (result.type == ResultType.SuccessWithAlert)
{
    model.GUID = result.GUID;
    . . .
}
else // Failure < no GUID assigned in diesem Fall
{
    . . .

...but I don't grok how they are expected to have values in the form's submit handler, where they are referenced:

    . . .
    if (resultsText == "0") {
        $("#NumberOfResults").css("color", "red");
    } else {
        var href = '/@ConfigurationManager.AppSettings["ThisApp"]/TLDCriteria/LoadReport';
        // report_parms (sic) is referenced from LoadReport
        var report_parms = {
            GUID: "@Model.GUID",
            SerialNumber: "@Model.SerialNumber",
            ReportName: "@Model.ReportName"
        };
        window.open(href, "report_window", "resizable=1, width=850, left=" + (screen.width / 2 - 425));
    }
. . .

IOW: When does the Controller's [HttpPost] ActionResult get fired? Before the form submit event finishes? If so, when exactly?

If not, why is this code referencing those model members which could not have been assigned values yet (and when they are empty strings, as they currently are, it causes the whole kit and kaboodle to ceremoniously but ignominiously fail)?

UPDATE

*report_parms* are, as the comment notes, referenced from LoadReport, which is a Silverlight-infested file with:

<script type="text/javascript">
    function get_user_name() {
        return "@User.Identity.Name";
    }

    function get_xml_data() {
        return window.opener.xml_data;
    }

    function get_receipt_parms() {
        return window.opener.receipt_parms;
    }

    function get_report_parms() {
        return window.opener.report_parms;
    }
</script>

Stepping through it in Firefox > Script, it indeed shows the values of those three members of "report_parms" being empty strings; I'm not surprised, but again: is there a reason why somebody would expect those vals to be populated at this point - and do they need to be? If so, how?

UPDATE 2

MisterJames,

Yes, there is a:

return View(model);

...at the end of the [HttpPost] ActionResult, but it turns out it is never reached; apparently another problem is preventing that from occurring; acutally, it seems like kind of a "chicken-and-egg" problem: I was thinking that the reason this code:

DatasourceCreatorResultContract result = Operations.CreateDataSource(criteria);

...was failing was because the criteria object passed to CreateDataSource() was not quite right, and that was because those report_parms were empty; But it might be, OTOH, that the problem lies elsewhere. To wit: the "if (result.type == ResultType.Success)" line is never reached, because the code fails due to lack of proper authentication, apparently.

I thought this was caused by my report_parms problem, but perhaps my report_parms problem is caused by this!

The entire section of code is:

        . . .
    model.SerialNumber = Serial.ToString();
    model.ReportName = "ReceiptCollection";

    DatasourceCreatorResultContract result = Operations.CreateDataSource(criteria);

    if (result.type == ResultType.Success) // <-- this is not reached; "conn.Open();" in another class fails before it can be
    {
        model.GUID = result.GUID;
        model.DataSourceSuccess = true;
        model.NumberOfResults = result.Count;
        model.message = String.Format("{0} results", result.Count);
    }
    else if (result.type == ResultType.SuccessWithAlert)
    {
        model.GUID = result.GUID;
        model.DataSourceSuccess = true;
        model.NumberOfResults = result.Count;
        model.message = result.Message;
    }
    else // Failure
    {
        model.DataSourceMessage = result.Message;
        model.DataSourceSuccess = false;
        model.NumberOfResults = 0;
        model.message = result.Message;
    }
}
return View(model);

解决方案

Clay, you'll need to make sure that after populating the model that the controller actually passes the values to the view. You should see something like:

return View(model);

Your view is typed to that model (which I saw from your other question), and therefore when you pass in the model to the View method, you're actually instructing the framework to work with that set of data as it builds the view.

I agree that some of this seems magical, but keep in mind that these are just classes and methods. Your controller inherits from a base controller that has a method called "View" on it. What you're doing is passing an object to that method, which in turn injects that into the Razor view engine to generate a result - an HTML page that is rendered and returned to the browser.

To your question, the form submit (which happens on the client) and the invocation of the ActionResult (which happens on the server) are only connected by virtue of your web browser making a request and paying attention to the result. The form is fully submitted as part of your browser's request. The method is not invoked until the server receives the full request (and creates an instance of your controller, etc).

Your typical Asp.Net MVC flow might be something like:

  1. A browser request results in ActionResult method being invoked. Being a form, you likely load up a view model and pass it to the view.
  2. The view is processed by the Razor view engine. The model that is passed in will be supplied to the engine and used throughout the view.
  3. The browser (erm...user) submits the form. The values are put into a request object and form encoded.
  4. The MVC framework spins up again, creates a controller, uses model binding to push the form-encoded values into your parameters - if it can - and invokes your HttpPost method.

If you are not seeing the values in the view (the HTML resultant page) in step 2, it is best to set a breakpoint in step 1 where you can evaluate why the model is not being populated.

Hope this helps some. If you want a more granular view of the pipeline, you can check out this post: Asp.Net MVC Pipeline.

Cheers.

这篇关于是否控制器的HttpPost ActionResult的获取过程中所谓的在,启动或表单提交事件之后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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