Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I am trying to make HTTPPOST from webclient using EXT.NET libraries.

Here is how my call looks like

 <ext:Button ID="dugmeOdobri" runat="server" Text="Odobri Narudžbe" Icon="Accept" >
                    <DirectEvents>
                        <Click 
                            Success="sayHello(result);"
                            Url="http://dnndev.me:9010/DesktopModules/WebApiSpn/API/odobrenje/odobri" 
                            Type="Load" 
                            Method="POST" 
                            CleanRequest="true" 
                            Json="true" >
                            <ExtraParams>
                                <ext:Parameter Name="Values" Value="Ext.encode(#{DocumentsGrid}.getRowsValues({selectedOnly:true}))" Mode="Raw" />
                            </ExtraParams>
                        </Click>
                    </DirectEvents>
                 </ext:Button>  

Fiddler and Chromes's F11 capture call like this

{"Values":"[{\"ID\":6894443,
            \"SkladisteSifra\":\"001\",
            \"SkladisteNaziv\":\"Skladi\\u0161te Veleprodaje RGT\",
            \"StatusCode\":null,
            \"StatusDescription\":\"Aktivna\",
            \"Broj\":\"000009\",
            \"Datum\":\"2013-06-13T00:00:00\",
            \"PartnerSifra\":\"24055\",
            \"PartnerNaziv\":\"15 APRIL\",
            \"Napomena\":\"UGALJ\",
            \"TipDokumenta\":\"VEL - Narud\\u017eba dobavlja\\u010du-slikovnica  \",
            \"Odobriti\":true,
            \"Editable\":true}
            ]"}

When I paste that string to json2csharp I get poco like this :

public class RootObject
{
    public string Values { get; set; }
}

What I want is to deserialize my request to poco like this:

public class RootObject
{
    public int ID { get; set; }
    public string SkladisteSifra { get; set; }
    public string SkladisteNaziv { get; set; }
    public object StatusCode { get; set; }
    public string StatusDescription { get; set; }
    public string Broj { get; set; }
    public string Datum { get; set; }
    public string PartnerSifra { get; set; }
    public string PartnerNaziv { get; set; }
    public string Napomena { get; set; }
    public string TipDokumenta { get; set; }
    public bool Odobriti { get; set; }
    public bool Editable { get; set; }
}

How should looks like method on server side ? Here what I tried so far one of n combination in last 3 hours

        [HttpPost]
        [AllowAnonymous]
        public string odobri(HttpRequestMessage  request)
        {
            Dictionary<string,string> values = request.Content.ReadAsAsync<Dictionary<string, string>>().Result;

            narudzbeZaOdobriti c = null; ;
            foreach (string s in values.Values)
            {
                 c = Newtonsoft.Json.JsonConvert.DeserializeObject<narudzbeZaOdobriti>(s);
                 break;
            }
            return c.Broj;

        }

I can't get where I do wrong, Is my call to Web Api missing something ?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

You can simply try this :

public string odobri(HttpRequestMessage request)
{
    Dictionary<string, string> values = request.Content.ReadAsAsync<Dictionary<string, string>>().Result;

    RootObject c = null;
    foreach (string s in values.Values)
    {
        List<RootObject> tmp = Newtonsoft.Json.JsonConvert.DeserializeObject<List<RootObject>>(s);
        c = tmp.First();
        break;
    }
    return c.Broj;

}
share|improve this answer
    
Thanx a lot, May I ask you to we together try Deserialize to List directly without deseriazling first to Dictionary<string,string>. Your first answer before edid point me to I can deseriazle to List<RootObject> first. That aprouch is more liked to me. Thany again – adopilot Jun 13 '13 at 14:43

Are you forced to use HttpRequestMessage?

Here I have prepared an MVC example.

View

<%@ Page Language="C#" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!X.IsAjaxRequest)
        {
            Store store = this.GridPanel1.GetStore();
            store.DataSource = new object[] 
            { 
                new SomeEntity()
                {
                    Test1 = "test11",
                    Test2 = "test12",
                    Test3 = "test13"   
                },
                new SomeEntity()
                {
                    Test1 = "test21",
                    Test2 = "test22",
                    Test3 = "test23"   
                },
                new SomeEntity()
                {
                    Test1 = "test31",
                    Test2 = "test32",
                    Test3 = "test33"   
                }
            };
        }
    }
</script>

<!DOCTYPE html>
<html>
<head runat="server">
    <title>Ext.NET v2 Example</title>
</head>
<body>
    <ext:ResourceManager runat="server" />

    <ext:Button runat="server" Text="Submit selected rows" Icon="Accept">
        <DirectEvents>
            <Click
                Success="alert(Ext.encode(result));"
                Url="/Aspx/SubmitSelected"
                Method="POST">
                <ExtraParams>
                    <ext:Parameter 
                        Name="values" 
                        Value="Ext.encode(#{GridPanel1}.getRowsValues({ selectedOnly:true }))" 
                        Mode="Raw" />
                </ExtraParams>
            </Click>
        </DirectEvents>
    </ext:Button>

    <ext:GridPanel ID="GridPanel1" runat="server">
        <Store>
            <ext:Store runat="server">
                <Model>
                    <ext:Model runat="server">
                        <Fields>
                            <ext:ModelField Name="Test1" />
                            <ext:ModelField Name="Test2" />
                            <ext:ModelField Name="Test3" />
                        </Fields>
                    </ext:Model>
                </Model>
            </ext:Store>
        </Store>
        <ColumnModel runat="server">
            <Columns>
                <ext:Column runat="server" Text="Test1" DataIndex="Test1" />
                <ext:Column runat="server" Text="Test2" DataIndex="Test2" />
                <ext:Column runat="server" Text="Test3" DataIndex="Test3" />
            </Columns>
        </ColumnModel>
        <SelectionModel>
            <ext:RowSelectionModel runat="server" SelectedIndex="1" Mode="Simple" />
        </SelectionModel>
    </ext:GridPanel>
</body>
</html>

Controller

using System.Collections.Generic;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;

namespace Work2MVC.Controllers
{
    public class AspxController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult SubmitSelected(string values)
        {
            List<SomeEntity> data = JSON.Deserialize<List<SomeEntity>>(values);

            return this.Direct(data.Count);
        }
    }
}

SomeEntity.cs

public class SomeEntity
{
    public string Test1 { get; set; }
    public string Test2 { get; set; }
    public string Test3 { get; set; }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.