views:

268

answers:

1

I'm trying to create an editable grid using Asp.Net MVC 2 and Silverlight (specifically a grid that displays info from a db and allows users to update that info).

So far I've managed to put a silverlight grid on an a view, using this technique

However I have no way of getting the updated data from the silver light grid. Is there anyway to get these values posted back to my controller?

I'm pretty new to Asp.Net MVC and I'm really only getting started using silverlight.

Thanks for any help!

A: 

The first thing you need to do is serialize back to JSON:-

(Assumption you use ToArray() on a ObservableCollection of MyItem objects)

 public string SerialiseToJSON(MyItem[] myItems)
 {
        //Create a stream to serialize the object to.
        MemoryStream ms = new MemoryStream();

        // Serializer the User object to the stream.
        DataContractJsonSerializer ser = new DataContractJsonSerializer(MyItem[]);
        ser.WriteObject(ms, myItemsArray);
        byte[] json = ms.ToArray();
        ms.Close();
        return Encoding.UTF8.GetString(json, 0, json.Length);
 }

Now you can use the WebClient class to send the JSON string back.

WebClient web = new WebClient();

web.UploadStringAsync(new Uri("/yourcontroller/jsonReceiver", UriKind.Relative));

Now I don't know MVC all that well but I believe you can annotate a controller action method so that it can accept a http POST of JSON data and it'll do the deserialisation for you.

AnthonyWJones
Thanks for that. I think its got me most of the way. However I'm having a problem getting the web.UploadStringAsync to hit my controller successfully - I just keep getting not found exception on the UploadStringCompletedEventArgs in the call back. I think I'll post a separate question on this.
bplus
got it working...thanks again...
bplus
@bplus: it would help others if you describe anything additional you had to do to get it working.
AnthonyWJones