简体   繁体   中英

How to pass “NULL” value into nullable field during call from JavaScript to ASP.NET WebService

How to pass "NULL" value into nullable field during call from JavaScript to ASP.NET WebService?

I call method in the following way from JavaScript:

ws_data.SaveTasks(eval({"MyValue":"null"}), OnSaveComplete, OnError, OnTimeOut);

When I do, I receive an error (framework calls OnError):

null is not a valid value for Int32

If instead of "null" I pass valid integer - everything works fine.

Please advise: What I do wrong? How to pass 'null' value which will be 'translated' into a NULL .NET object?

Any ideas are welcome!

PS Here are some details.

Declaration of essential part of MyData:

public class MyData
{
    ...
    public int? MyValue { get; set; }
}

Declaration of ASP.NET web service:

[WebService(Namespace = "...")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class ws_data : WebService
{
    [WebMethod(true)]
    public object SaveData(MyData data)
    {...}
}

And ws_data is declared via ScriptManager:

    <asp:ScriptManager runat="server">
        <Services>
            <asp:ServiceReference Path="/ws_data.asmx" />
        </Services>
    </asp:ScriptManager>

This is because you're passing the string null instead of the non-value null. Remove the quotes to make it an actual null.

{"MyValue": null}

I'm not sure about your eval either, I think it can be omitted as eval is supposed to take a string an interpret it as JavaScript.

ws_data.SaveTasks({"MyValue": null}, OnSaveComplete, OnError, OnTimeOut);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM