Asp.net mvc 将 C# 对象传递给 Javascript [英] Asp.net mvc passing a C# object to Javascript

查看:28
本文介绍了Asp.net mvc 将 C# 对象传递给 Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 C# 类说选项更像是 AjaxOptions.

I have c# class say options more like AjaxOptions.

public class options
{
   public string Url {get;set;}
   public string httpMethod {get;set}
}

和这样的javascript函数

and a javascript function like this

function dosomething(obj)
{
   if (obj.Url!="" and obj.HttpMethod=="something")
    loadsomething();

}

现在在我的控制器操作中

Now in my Controller action

 public class mycontroller : controller
 {
    public ActionResult WhatToDo()
    {
       options obj = new options{Url="someurl"};
       return PartialView(obj);
    }


 }

在我看来,我需要这种对象类型的字符串,我应该能够将其传递给我的方法.

in my view I need this object kind of string which i should be able to pass to my method.

@model options

<script>
   dosomething(@model.SomeFunctionToConverToString())

</script>

所以我需要这个 SomeFunctionToConverToString 方法,我会将这个对象转换为字符串.

So I need this SomeFunctionToConverToString method which i will convert this object to string.

谢谢

推荐答案

您应该能够像使用视图中的任何其他模型属性输出一样使用它.在JS函数中引用你想传递的属性即可.

You should be able to use it like you would any other output of a model property in your view. Just reference the property that you want to pass in the JS function.

@model options

<script>
   dosomething('@(model.Url)');
</script>

有关在 JS 中使用 Razor 的更多信息,请参阅这篇文章

See this post for more information on using Razor inside of JS

EDIT - 可能会引起您注意的是,如果您的 URL 被 Razor 使用上述的 HTML 编码破坏,您可以使用 @Html.Raw() 函数将传递 Url 属性而不对其进行 HTML 编码.

EDIT - Something that might catch you is that if your URL get's broken from the HTML encoding that Razor does using the above, you can use the @Html.Raw() function which will pass the Url property without HTML encoding it.

<script>
   dosomething('@Html.Raw(model.Url)');
</script>

编辑 2 - 和 另一个 SO 帖子 来救援!您很可能希望将模型转换为 JSON,以便在 Javascript 函数中使用.所以...为了做到这一点 - 你需要在你的视图模型中使用一些东西来处理 JSON 对象.

EDIT 2 - And another SO post to the rescue! You are going to most likely want to convert your model to JSON in order to use in a Javascript function. So...in order to do that - you will need something in your view model to handle a JSON object.

public class optionsViewModel
{
   public options Options{get;set;}
   public string JsonData{get;set;}
}

并在您的控制器中:

public class mycontroller : controller
 {
    public ActionResult WhatToDo()
    {
       options obj = new options{Url="someurl"};
       var myViewModel = new optionsViewModel;
       myViewModel.options = obj;
       var serializer = new JavaScriptSerializer();
       myViewModel.JsonData = serializer.Serialize(data);
       return PartialView(myViewModel);
    }
 }

最后是视图:

@model optionsViewModel

<script>
   dosomething('@model.JsonData')

</script>

使用此方法,您的功能将按预期工作:

Using this method, then your function will work as expected:

function dosomething(obj)
{
   if (obj.Url!="" and obj.HttpMethod=="something")
    loadsomething();
}

EDIT 3 可能是最简单的方法?与编辑 2 相同的前提,但是这是使用视图对模型进行 JsonEncode.无论这应该在视图、控制器还是存储库/服务层中完成,双方可能都有一些很好的论据.但是,为了在视图中进行转换...

EDIT 3 Potentially the simplest way yet? Same premise as edit 2, however this is using the View to JsonEncode the model. There are probably some good arguments on either side whether this should be done in the view, controller, or repository/service layer. However, for doing the conversion in the view...

@model options

<script>
   dosomething('@Html.Raw(Json.Encode(Model))');
</script>

这篇关于Asp.net mvc 将 C# 对象传递给 Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆