[CLOSED] DirectMethod problem

  1. #1

    [CLOSED] DirectMethod problem

    View code:

    @model System.Collections.IEnumerable
    @{
        ViewBag.Title = "My Report";
        var X = Html.X();
    }
    
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title></title>
        <script type="text/javascript">
            var jsExport2Excel = function () {
                Ext.net.DirectMethod.request({
                    url: "/Report/ProduceEEReportWithDirectorInformation",
                    cleanRequest: true,
                    isUpload: true,
                    formId: "myForm",
                    success: function (result) {
                        Ext.Msg.Show("Excel file export successfull");
                    },
                    error: function (result) {
                        Ext.Msg.Show("Export unsuccessfull");
                    }
                });
            };
        </script>
    </head>
    
    <body>
        @section sectionCenterBody
    {
            
    }
    
        @section  OptionalContent
        {
        <script src="~/Scripts/Report/EEReport.js"></script>
        <div>
            <form id="myForm">
                <div align="center" style="Position:relative; top: 200px">
                    @(X.FileUploadField().ID("myUploadField").ButtonOnly(true).Icon(Icon.Attach).ButtonText("Produce EE Report With Director Information")
                .Listeners(l => l.Change.Fn = "jsExport2Excel"))
                </div>
            </form>
        </div>
            }   
    </body>
        
    </html>
    Controller Code:
    [DirectMethod]
    public ActionResult ExportExcel()
            {
                
                FileUploadField fileUploadField = X.GetCmp<FileUploadField>("myUploadField");
                HttpPostedFile file = fileUploadField.PostedFile;
                if (fileUploadField.HasFile)
                {
                    string filePath = file.FileName;
                    //string path = this.HttpContext.Server.MapPath("~") + "\\" + fileName;
                    string dateInWordFormat = GetDateInSpecificFormat();
                    string tmpFilePath = Constants.ExcelDetails.DefaultSavePath + Constants.ExcelDetails.DefaultFileName + ".xlsx";
                    string finalFilePath = Server.MapPath(tmpFilePath);
                    file.SaveAs(finalFilePath);
                    ReadSavedExcel();
                    WriteNewExcelAndOpen();
                }
                else
                {
                    X.Msg.Show(new MessageBoxConfig
                    {
                        Buttons = MessageBox.Button.OK,
                        Icon = MessageBox.Icon.ERROR,
                        Title = "Fail",
                        Message = "No file uploaded"
                    });
                }
                DirectResult result = new DirectResult();
                result.IsUpload = true;
                return result;
            }
    Above code runs fine if user does everything as required, but if user uploads empty excel file then I want my JavaScripts DirectMethod's error function to be called upon and gives a message to user "Export Unsuccessfull".

    But, it's not happening that way, when user uploads empty excel file then the exception thrown in the controller action method does not goes back to javascripts error function and rather just shows the bad response error window to the user and also gives an option to open uploaded file in the json format with the direct methods name as the filename.


    Can anybody guide me what's wrong I am doing here?

    Thanks
    Last edited by Daniil; Mar 06, 2015 at 5:30 AM. Reason: [CLOSED]
  2. #2
    Hi @sharmav1,

    There is no an error handler. There is a failure.
    http://examples2.ext.net/#/Events/Di...hods/Overview/

    Also there is no Ext.Msg.Show. It is Ext.Msg.show(lowercase "s"). But it expects another set of parameters:
    http://docs.sencha.com/extjs/4.2.1/#...ox-method-show

    I guess you want to use .alert().
    http://docs.sencha.com/extjs/4.2.1/#...x-method-alert

    Please try to replace the error handler with.
    failure: function (result) {
        Ext.Msg.alert("Export unsuccessfull", "Error message");
    }
  3. #3
    Hi @Daniil,

    Thanks for your reply.

    First thing: As you suggested "failure" did work for me. But, one thing in failure did not work and i.e. the result parameter passed to it is not getting the value passed to it from caller, and instead it always gives the value of "NORESPONSE" (Attachment 22041)

    second thing: I am always getting a pop-up to ask "Do you want to open or save "file name (controller's action method name with some random characters in json format)" which is not what I expect when anything goes wrong with the method or there is some exception in the method.
    I just want a simple error message to show to the user if some exception occurs in the method.

    Thanks
    Attached Thumbnails pagescreenshot.jpg  
  4. #4
    Please try to use a DirectResult's ErrorMessage.
    DirectResult result = new DirectResult();
    result.ErrorMessage = "Some ErrorMessage";
    Then a result argument of a failure hanlder should be that ErrorMessage's value.
  5. #5
    Hi @Daniil,

    It Worked, Thanks Very Much.

Similar Threads

  1. [CLOSED] DirectMethod's problem
    By softmachine2011 in forum 2.x Premium Help
    Replies: 1
    Last Post: Jun 06, 2012, 3:56 PM
  2. DirectMethod problem
    By abhijit in forum 1.x Help
    Replies: 4
    Last Post: Dec 23, 2011, 12:43 PM
  3. [CLOSED] DirectMethod with UserControl Problem
    By nhg_itd in forum 1.x Premium Help
    Replies: 1
    Last Post: Sep 21, 2011, 8:00 AM
  4. [CLOSED] DirectMethod.Request Problem
    By tansu in forum 1.x Premium Help
    Replies: 10
    Last Post: Oct 28, 2010, 8:18 AM
  5. Replies: 2
    Last Post: Oct 08, 2010, 7:09 PM

Tags for this Thread

Posting Permissions