我在序列化中后通过我的类对象,然后将其访问为空白的JavaScript。
我的班级
public class PMICheckListWrapper
{
public PMIMachine Machine { get; set; }
public List<PMICheckListResponse> CheckResponseList { get; set; }
}
控制器代码:
public ActionResult GetViewEditData(int machineID)
{
var result = Json(PMIComputerModel.GetViewEditDetails(machineID), JsonRequestBehavior.AllowGet);
ViewBag.MachineData = result.Data;
return View("PMIComputerList");
}
javaScript我正在访问它。
$(function(){
if('@ViewBag.MachineData' != null)
{
var data = '@ViewBag.MachineData';
alert(data);//this is coming empty
alert(jQuery.parseJSON(data)); // this is coming null
}
});
尝试使用
var data = @Html.Raw(@ViewBag.MachineData);
getVieweditDetails实际上有多确定吗?尝试:
public ActionResult GetViewEditData(int machineID)
{
var result = new JsonResult();
var data = PMIComputerModel.GetViewEditDetails(machineID);
if(data != null){
result.Data = data;
}
else {
// give some kind of feedback that there is no result
}
ViewBag.MachineData = result;
return View("PMIComputerList");
}