Asp.net MVC传递一个C#对象为Javascript [英] Asp.net mvc passing a C# object to Javascript

查看:215
本文介绍了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();

}

现在在我的控制器行动

 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里面剃刀

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

修改 - 这可能会抓住你的是,如果您的网址获取的从剃刀不使用上述的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 - 和<一个href=\"http://stackoverflow.com/questions/3365551/asp-net-mvc-how-to-convert-view-model-into-json-object\">another 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();
}

编辑3 潜在的最简单的方法了吗?同样的premise为编辑2,然而,这是使用视图JsonEn code模型。有可能是两侧一些好的论点这是否在视图,控制器或存储库/服务层来完成。然而,对于在视图执行转换...

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天全站免登陆