我有像下面的json对象
Extension = { "BookMarks": [{"Name":"User1","Number":"101"},{"Name":"User2","Number":"102"},{"Name":"User3","Number":"103"}]}
我想将此json字符串发送到我的控制器Action方法并反序列化数据
我想将数据传递给partialview
public ActionResult ExtensionsDialog(var data) { return PartialView(data); }
任何帮助
提前致谢..
解决方法
在您的视图中:
function SendData(){ var dataToSend = JSON.stringify(data); $.ajax({ type: "POST",url: '@Url.Action("YourAction","YourController")',dataType: "json",data: dataToSend,contentType: "application/json; charset=utf-8",}); } $("#Updatebtn").click(function () { sendData(); });
在你的模型:
public class YourModel { public String Name { get; set; } public int Number { get; set; } }
在您的控制器中:
[HttpPost] public ActionResult YourAction() { var resolveRequest = HttpContext.Request; List<YourModel> model = new List<YourModel>(); resolveRequest.InputStream.Seek(0,SeekOrigin.Begin); string jsonString = new StreamReader(resolveRequest.InputStream).ReadToEnd(); if (jsonString != null) { JavaScriptSerializer serializer = new JavaScriptSerializer(); model = (List<YourModel>)serializer.Deserialize(jsonString,typeof(List<YourModel>); } //Your operations.. }
希望这可以帮助.