[英]MVC Action Method & Javascript
我有一个简单的脚本视图:
<script type="txt/javascript">
function DoSomething(param)
{
alert(param);
}
</script>
无论如何我可以用控制器动作方法返回结果中的值来调用该函数吗?
编辑:
我真的希望用Entity Framework值创建/注册JavaScript类。 如果无法完成,我如何使用EntityFramework值创建/注册Javascript类?
好的,这就是事情:我已经有了这样的JS函数:
RegisterClasses(param1, param2)
{
......
}
我已经看到它通过Fiddler在其他地方完成,但不知道他们是如何做到的。
如何将值传递给JS函数以从EF中的值创建类?
你可以做的是将其添加到视图中:
<script type="text/javascript">DoSomething(@Model.Param)</script>
将Param
属性添加到该操作视图模型。
要使用EF类,您可以这样做:
<script type="text/javascript">
var jsModel = {
name: "@Model.Name",
age: @Model.Age
};
</script>
此外,你可以在这个答案之后将任何对象转换为json: 在视图中将.Net对象转换为JSON对象
希望能帮助到你。
您可以将JSON序列化到视图中:
@model MyViewModel
<script type="text/javascript">
var model = @Html.Raw(Json.Encode(Model));
// now the model variable is a JSON representation of your
// server side model that was passed by the controller action
// to this view and you could manipulate it as a standard js variable
// So assuming your model contains 2 properties Param1 and Param2 you
// could pass them to your javascript method
RegisterClasses(model.Param1, model.Param2);
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.