Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

c# - 从Javascript访问MVC的模型属性(Accessing MVC's model property from Javascript)

I have the following model which is wrapped in my view model(我有以下模型,它包含在我的视图模型中)

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; } } How do I access one of the above properties from Javascript?(如何从Javascript访问上述属性之一?) I tried this, but I got "undefined"(我试过这个,但我得到了“未定义”) var floorplanSettings = "@Model.FloorPlanSettings"; alert(floorplanSettings.IconsDirectory);   ask by Null Reference translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could take your entire server-side model and turn it into a Javascript object by doing the following:(您可以通过执行以下操作来获取整个服务器端模型并将其转换为Javascript对象:)

var model = @Html.Raw(Json.Encode(Model)); In your case if you just want the FloorPlanSettings object, simply pass the Encode method that property:(在您的情况下,如果您只想要FloorPlanSettings对象,只需传递Encode方法的属性:) var floorplanSettings = @Html.Raw(Json.Encode(Model.FloorPlanSettings));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...