Silverlight, Grids, MVC, HTTP Post
I'm trying to create an editable grid u开发者_Go百科sing 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!
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.
精彩评论