PDA

View Full Version : [CLOSED] Submit null as value of a string parameter



RCN
Dec 26, 2012, 6:18 PM
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

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;
}
}

Baidaly
Dec 26, 2012, 7:11 PM
Hello!

This behavior caused by ASP.NET MVC default ModelBinder. If you want to change it you should write your own ModelBinder.

RCN
Dec 26, 2012, 8:04 PM
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;
}

RCN
Dec 26, 2012, 8:08 PM
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';
}

Baidaly
Dec 27, 2012, 1:26 AM
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

Daniil
Dec 27, 2012, 4:12 AM
Hi everybody,

I agree with Daulet it is default behavior.

Here is a related discussion.
http://stackoverflow.com/questions/8448150/asp-net-mvc-receives-null-as-a-string-instead-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.

RCN
Dec 27, 2012, 10:55 AM
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.