如何将模型存储在隐藏的输入中并在我的javascript函数中访问它?
我将模型存储在隐藏字段中
string defaultFareListJson =
Newtonsoft.Json.JsonConvert.SerializeObject(@Model.DefaultFare);
@Html.Hidden("DefaultFareList", defaultFareListJson);
在我的Javascript函数中,我试图以:
的形式访问它function viewDefaultFareSchedule() {
var defaultFareObject = {}
defaultFareObject.Fares = jQuery.parseJSON($('#DefaultFareList').val());
alert(defaultFareObject.Fares[0].facility);
}
我的ViewModel如下:
public class NavBarCallerViewModel
{
public FareSchedViewModel DefaultFare { get; set; } = new FareSchedViewModel();
}
public class FareSchedViewModel {
public string facility{get;set;}
}
NH
答案 0 :(得分:0)
我能够解析隐藏的JSON字符串(" DefaultFareList":从模型转换):
var defaultFareString = document.getElementById('DefaultFareList').value;
var defaultFare = JSON.parse(defaultFareString);