Dec 16, 2014, 11:51 AM
[CLOSED] [#611] Can DirectResult support success false without requiring ErrorMessage to be set?
Hi,
Looking at DirectResult I see this:
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:
If there might be problems I have not considered, is it possible to refactor out the two lines into a virtual method:
Thanks!
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 likeif (!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]