can';t将javascript对象反序列化为c#
我想将模型从控制器动作传递到视图。。因此,我发送了对象列表,并将其转换为javascript对象,用于在google地图中显示标记。 然后,我希望单击标记时,将选定对象(选定标记)发送到控制器操作can';t将javascript对象反序列化为c#,javascript,ajax,asp.net-mvc,Javascript,Ajax,Asp.net Mvc,我想将模型从控制器动作传递到视图。。因此,我发送了对象列表,并将其转换为javascript对象,用于在google地图中显示标记。 然后,我希望单击标记时,将选定对象(选定标记)发送到控制器操作 @model List<FleetDesignerMVC.ViewModel.VehicleFullInformations> <body> <div style="width:100%; height:650px" id="map
@model List<FleetDesignerMVC.ViewModel.VehicleFullInformations>
<body>
<div style="width:100%; height:650px" id="map"></div>
</body>
<script>
function initMap() {
var initialLatLang = {lat: 36.862499, lng: 10.195556};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 8,
center: initialLatLang,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
//CONVERT CONROLLER MODEL TO JAVASCRIPT OBJECT
var model = @Html.Raw(Json.Encode(Model));
var infowindow = new google.maps.InfoWindow;
//CREATING MARKERS
for (i = 0; i < model.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(model[i].VehicleCurrentPosition.Latitude, model[i].VehicleCurrentPosition.Longitude),
map: map,
icon: model[i].Vehicle.MarkerPicture
});
google.maps.event.addListener(marker, 'click', (function (marker, i) {
return function () {
infowindow.setContent('<table>'+
'<tbody>'+
'<tr>'+
'<td><img src="' + model[i].Vehicle.VehiclePicture + '" alt="imageed" width="150" height="150" /></td>'+
'<td>'+
'<table>'+
'<tbody>'+
'<tr>'+
'<td><h5>' + model[i].Vehicle.Matricule + '</h5></td>'+
'</tr>'+
'<tr>' +
'<td>Date: ' + new Date(parseInt(model[i].VehicleCurrentPosition.TrackDate.substr(6))).getDate() + '/' + new Date(parseInt(model[i].VehicleCurrentPosition.TrackDate.substr(6))).getMonth() + '/' + new Date(parseInt(model[i].VehicleCurrentPosition.TrackDate.substr(6))).getFullYear() + '</td>' +
'</tr>' +
'<tr><td>Hour: ' + new Date(parseInt(model[i].VehicleCurrentPosition.TrackDate.substr(6))).getHours() + ':' + new Date(parseInt(model[i].VehicleCurrentPosition.TrackDate.substr(6))).getMinutes() + ':' + new Date(parseInt(model[i].VehicleCurrentPosition.TrackDate.substr(6))).getSeconds() + '</td></tr>' +
'<tr>'+
'<td>'+
'<p>Speed: ' + model[i].VehicleCurrentPosition.Speed + ' Km/H</p>'+
'</td>'+
'</tr>'+
'<tr><td> <button onclick="postData(\'' + model[i]+ '\')">Send</button> </td></tr>' +
'</tbody>'+
'</table>'+
'</td>'+
'</tr>'+
'</tbody >'+
'</table >');
infowindow.open(map, marker);
}
})(marker, i));
}
}
function postData(model) {
var f = {};
f.type = "POST";
f.url = '@Url.Action("Index", "VehicleDetails")';
f.contentType = "application/json; charset=utf-8";
f.data = JSON.stringify({model});
f.dataType = "json";
f.success = function (response) {
alert("success");
};
f.error = function (response) {
alert("failed");
};
$.ajax(f);
};
</script>
标记显示在地图中,但当我选择一个标记并单击“发送”按钮时,发送的对象类型为[object object],我试图对其进行反序列化,但出现错误“Primitive JSON non valide:object”
这是我的行动方法
[HttpPost]
公共操作结果索引(字符串模型)
{
var jss=新的JavaScriptSerializer();
var dataObject=jss.Deserialize(model);
Console.WriteLine(数据对象);
//…对数据对象执行某些操作
返回Json(“OK”);
}
有什么建议吗?
提前谢谢你,我的英语很差,很抱歉。stringify({model})似乎有点可疑。您正在此处创建一个对象,其中包含
model
。该对象看起来像:
{
型号:{
…你的实际模型。。。
}
}
您可能只需要f.data=JSON.stringify(model)
,它不会在将JSmodel
发送到控制器之前将其包装到另一个对象中
此外,在调试器中,可以在var dataObject=jss.Deserialize(model)行上设置断点编码>并检查C语言中的模型
,看看它是否包含您期望的内容
更新
Send
几乎肯定不是你想要的。加载页面后,检查开发工具中的按钮元素,您将看到它可能看起来像…
您可能希望执行类似于…
的操作,然后:
函数postData(modelIndex){
设postModel=model[modelIndex];
...
f、 data=JSON.stringify(postModel);
...
}
hey@mlibby谢谢,当我发送模型而不将其放在大括号{}中时,我在控制器操作中得到一个空值!!我还试着调试代码以查看“model”的值,结果就是[object]!谢谢,现在可以用了!我改变了你提到的按钮,现在它工作了。请注意:我将对象放在大括号=>f.data=JSON.stringify({model})之间,并将操作参数类型更改为对象名称,而不是string=>public ActionResult Index(VehicleFullInformations model)。再次感谢:)
public class VehicleFullInformations
{
public Vehicle Vehicle { get; set; }
public Brand VehicleBrand { get; set; }
public Model VehicleModel { get; set; }
public VehicleType VehicleType { get; set; }
public GpsTrack VehicleCurrentPosition { get; set; }
public FuelType VehicleFuelType { get; set; }
}
[HttpPost]
public ActionResult Index(string model)
{
var jss = new JavaScriptSerializer();
var dataObject = jss.Deserialize<VehicleFullInformations>(model);
Console.WriteLine(dataObject);
// .. do something with data object
return Json("OK");
}