Silverlight, Grids, MVC, HTTP Post

半腔热情 提交于 2019-12-11 06:10:31

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/2667068/silverlight-grids-mvc-http-post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!