我的视图模型中包含以下模型
public class FloorPlanSettingsModel { public int Id { get; set; } public int? MainFloorPlanId { get; set; } public string ImageDirectory { get; set; } public string ThumbnailDirectory { get; set; } public string IconsDirectory { get; set; } }
如何从Javascript访问上述属性之一?
我尝试了这个,但是我得到了“未定义”
var floorplanSettings = "@Model.FloorPlanSettings"; alert(floorplanSettings.IconsDirectory);
您可以通过以下操作将整个服务器端模型转换为Javascript对象:
var model = @Html.Raw(Json.Encode(Model));
在您的情况下,如果只需要FloorPlanSettings对象,则只需传递Encode方法that属性:
Encode
var floorplanSettings = @Html.Raw(Json.Encode(Model.FloorPlanSettings));