[CLOSED] Submit null as value of a string parameter

  1. #1

    [CLOSED] Submit null as value of a string parameter

    When the Submit button is clicked, the Submit DirectMethod is called passing null for the two parameters. The first parameter is a string and the second one is a nullable int.

    i would like to know why paramA equals "null" instead of null while paramB equals null, since both parameters are nullable

    Thanks in advance

    Click image for larger version. 

Name:	example 016.png 
Views:	128 
Size:	28.3 KB 
ID:	5314

    1 - View
    <!DOCTYPE html>
    <html>
    <head id="Head1" runat="server">
    <script type="text/javascript">
        var submit = function () {
            var nullParameter = null;
    
            Ext.net.DirectMethod.request({
                url: "/Example/Submit",
                cleanRequest: true,
                params: {
                    paramA: nullParameter,
                    paramB : nullParameter
                },
                success: function (result) {
                    alert('Success');
                },
                failure: function (result) {
                    alert(result);
                }
            });
        }
    </script>
    </head>
    <body>
        <ext:ResourceManager ID="ResourceManager1" runat="server" />
        <ext:Button Text="Submit" runat="server">
            <Listeners>
                <Click Handler="submit();" />
            </Listeners>
        </ext:Button>
    </body>
    </html>
    2 - Controller
    public class ExampleController : System.Web.Mvc.Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    
        public AjaxResult Submit(string paramA, Nullable<int> paramB)
        {
            AjaxResult result = new AjaxResult();
    
            if (paramA == "null")
            {
                result.ErrorMessage = "ParamA parameter is not null on Server";
            }
    
    
            return result;
        }
    }
    Last edited by Daniil; Dec 27, 2012 at 11:02 AM. Reason: [CLOSED]
  2. #2
    Hello!

    This behavior caused by ASP.NET MVC default ModelBinder. If you want to change it you should write your own ModelBinder.
  3. #3
    In my opinion, it's not caused by ASP.NET MVC default ModelBinder. Take a look on the following code, the Submit method has a new parameter paramC that it's not initialized ducting DirectMethod request. i get the following result when the button Submit is clicked:

    • paramA = "null"
    • paramB = null
    • paramC = null



    note that both paramA and paramB are strings.

    <script type="text/javascript">
        var submit = function () {
            var nullParameter = null;
    
            Ext.net.DirectMethod.request({
                url: "/Example/Submit",
                cleanRequest: true,
                params: {
                    paramA: nullParameter,
                    paramB : nullParameter
                },
                success: function (result) {
                    alert('Success');
                },
                failure: function (result) {
                    alert(result);
                }
            });
        }
    </script>
    public AjaxResult Submit(string paramA, Nullable<int> paramB, string paramC)
    {
        AjaxResult result = new AjaxResult();
    
        if (paramA == "null")
        {
            result.ErrorMessage = "ParamA parameter is not null on Server";
        }
        return result;
    }
  4. #4
    I am not sure whether DirectMethod request use Ext.JSON doEncode method when encoding the parameters, but in case of it uses the error can be found at line 4.

    I'll investigate and keep you posted.

    doEncode = function(o, newline) {
        // http://jsperf.com/is-undefined
        if (o === null || o === undefined) {
            return "null";
        } else if (Ext.isDate(o)) {
            return Ext.JSON.encodeDate(o);
        } else if (Ext.isString(o)) {
            return Ext.JSON.encodeString(o);
        } else if (typeof o == "number") {
            //don't use isNumber here, since finite checks happen inside isNumber
            return isFinite(o) ? String(o) : "null";
        } else if (Ext.isBoolean(o)) {
            return String(o);
        }
        // Allow custom zerialization by adding a toJSON method to any object type.
        // Date/String have a toJSON in some environments, so check these first.
        else if (o.toJSON) {
            return o.toJSON();
        } else if (Ext.isArray(o)) {
            return encodeArray(o, newline);
        } else if (Ext.isObject(o)) {
            return encodeObject(o, newline);
        } else if (typeof o === "function") {
            return "null";
        }
        return 'undefined';
    }
  5. #5
    In my opinion, it's not caused by ASP.NET MVC default ModelBinder. Take a look on the following code, the Submit method has a new parameter paramC that it's not initialized ducting DirectMethod request.
    Actually, it's default behaviour of MVC 3. It's very easy to check just call any MVC 3 GET action with following parameters:

    http://mysite.com/controller/action?paramA=null&paramB=null
    And in your action you will see the following:

    public ActionResult Submit(string paramA, Nullable<int> paramB, string paramC) // <-- paramA = "null", paramB = null, paramC = null
  6. #6
    Hi everybody,

    I agree with Daulet it is default behavior.

    Here is a related discussion.
    http://stackoverflow.com/questions/8...nstead-of-null

    Possible solutions are:

    1. Setting "json: true" in the config of the request.

    or

    2. Do not send a null parameter at all.

    or

    3. Decode "null" manually within a controller action.
  7. #7
    Thank you guys by the precise and concise information regarding this issue.

    i will pick solution number 1, it suits my needs like a charm.

    Please mark this thread as closed.

Similar Threads

  1. Submit form by parameter
    By nukarsoft in forum 2.x Help
    Replies: 2
    Last Post: Dec 21, 2012, 11:02 AM
  2. Replies: 0
    Last Post: Dec 02, 2011, 12:23 AM
  3. Cant send html string as parameter to AjaxMethod
    By masudcseku in forum 1.x Help
    Replies: 9
    Last Post: Jul 19, 2011, 10:38 PM
  4. [CLOSED] grid parameter is null for prepareCommand function in 1.0.
    By vedagopal2004 in forum 1.x Legacy Premium Help
    Replies: 5
    Last Post: Feb 14, 2011, 7:47 AM
  5. Two Grids - submit additional parameter [0.82]
    By roszman in forum 1.x Help
    Replies: 2
    Last Post: Jun 24, 2010, 7:11 AM

Posting Permissions