[CLOSED] [#611] Can DirectResult support success false without requiring ErrorMessage to be set?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1

    [CLOSED] [#611] Can DirectResult support success false without requiring ErrorMessage to be set?

    Hi,

    Looking at DirectResult I see this:

            public override void ExecuteResult(ControllerContext context)
            {
                DirectResponse response = new DirectResponse();
    
                response.Result = this.Result;
                response.IsUpload = this.IsUpload;
    
                response.Success = ResourceManager.AjaxSuccess;
                response.ErrorMessage = ResourceManager.AjaxErrorMessage;
    
                if (!string.IsNullOrEmpty(this.ErrorMessage))
                {
                    response.Success = false;
                    response.ErrorMessage = this.ErrorMessage;
                }
                else
                {                
                    if (!string.IsNullOrEmpty(this.Script))
                    {
                        response.Script = this.Script;
                    }
    
                    if (this.ExtraParamsResponse.Count > 0)
                    {
                        response.ExtraParamsResponse = this.ExtraParamsResponse.ToJson();
                    }
                }
                
                response.Return();
            }
    For the response.Success to be false, I must set the ErrorMessage.

    However, I have times where I set the error message on the client side (JavaScript) as I format some complicated HTML which I cannot easily do in code behind, because of the various bits of data and state that the JavaScript has.

    Would it be possible to extend DirectResult in the following way:
    1. Have a property, Success, (default to true for backward compatibility)
    2) Extend the check above so if !Success || !string.IsNullOrEmpty(this.ErrorMessage) do what is currently being done

    Something like this:

           private bool _success = true;
    
           public bool Success
           {
               get { return _success; }
               set { _success = value; }
            }
    
            ... everything else as before ...
    
            public override void ExecuteResult(ControllerContext context)
            {
                DirectResponse response = new DirectResponse();
    
                response.Result = this.Result;
                response.IsUpload = this.IsUpload;
    
                response.Success = ResourceManager.AjaxSuccess;
                response.ErrorMessage = ResourceManager.AjaxErrorMessage;
    
                if (!this.Success || !string.IsNullOrEmpty(this.ErrorMessage)) // <-- only modification to this method is here
                {
                    response.Success = false;
                    response.ErrorMessage = this.ErrorMessage;
                }
                else
                {                
                    if (!string.IsNullOrEmpty(this.Script))
                    {
                        response.Script = this.Script;
                    }
    
                    if (this.ExtraParamsResponse.Count > 0)
                    {
                        response.ExtraParamsResponse = this.ExtraParamsResponse.ToJson();
                    }
                }
                
                response.Return();
            }
    Or might this cause some unforeseen issues?

    If there might be problems I have not considered, is it possible to refactor out the two lines into a virtual method:

    if (!string.IsNullOrEmpty(this.ErrorMessage))
    into something like

    if (!this.HasError())
    
    ... etc ...
    
    protected virtual bool HasError()
    {
        return !string.IsNullOrEmpty(this.ErrorMessage)
    }
    Then I can create a subclass and just override the HasError method and include my own success property in my subclass...

    Thanks!
    Last edited by Daniil; Dec 18, 2014 at 3:00 PM. Reason: [CLOSED]

Similar Threads

  1. MessageBus and DirectEvent.Success = false
    By Spamme in forum 2.x Help
    Replies: 1
    Last Post: Aug 13, 2013, 12:24 PM
  2. Replies: 2
    Last Post: Jan 10, 2013, 2:33 PM
  3. Replies: 3
    Last Post: Jan 03, 2013, 6:57 PM
  4. Replies: 3
    Last Post: Jul 16, 2012, 7:36 AM
  5. Replies: 1
    Last Post: Jun 07, 2010, 7:19 AM

Posting Permissions