获得一个MVC2可变进的Jquery [英] Getting a MVC2 variable into Jquery

查看:161
本文介绍了获得一个MVC2可变进的Jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在jQuery来操纵一个asp.net MVC2视图变量...

I am attempting to manipulate an asp.net MVC2 view variable in Jquery...

(function () {
    debugger;
    var $a = 10;
    var $b = '<%:toggle %>';
    var $c = 12;
});

正如你可能已经猜到了,我试图操纵我在jQuery中的ASP.net MVC2视图发起切换变量。

As you may have guessed, I am attempting to manipulate the toggle variable that I have initiated in an ASP.net MVC2 view in Jquery.

似乎我上面那样没有在所有的工作。我尝试加载该页面,每次我尝试打开它炸毁。任何想法,为什么这可能是?注意......即使我摆脱''(撇号)我的页面仍然炸毁。我也试图使公众质疑的变量。这似乎没有任何工作。

What I did above doesn't seem to work at all. The page that I attempt to load, blows up every time I try and open it. Any ideas why that might be? Note... Even if I get rid of the ' ' (apostrophes) my page still blows up. I also attempted to make the variable in question public. That doesn't seem to work either.

更新预期VS我所得到的
我不指望得到这个...

UPDATE EXPECTED vs WHAT I GET I don't expect to get this...

下面是我的期望

Here is what I do expect

我有一个很难得到正在发送到页面的错误消息。我要弄清楚它的生成地点和尝试,并从那里追了下去。我的同事建立了某种JavaScript错误的路由方案对这个特定页面。而且我有一个小麻烦破译是怎么回事。如果我知道我的错误可能会在良好的状态。

I am having a hard time getting the error message that is being sent to the page. I have to figure out where its being generated and try and chase it down from there. My coworker set up some kind of Javascript error routing scenario for this particular page. And I am having a little trouble deciphering what is going on. If I knew the error I would probably be in good shape.

更新2只是想出了错误

UPDATE 2 Just figured out the error

TemplateInfo.aspx(371):错误CS0103:名称'切换'不在当前情况下存在...

TemplateInfo.aspx(371): error CS0103: The name 'toggle' does not exist in the current context...

我猜测变量以某种方式公开或投入使用范围莫名其妙。怎么能。据我所知,你不能创建MVC2公共变量?你能吗?

I am guessing that variable has to be somehow made public or put into scope somehow. How can. To my knowledge you can't create public variables with MVC2? Can you?

推荐答案

它看起来像您尝试使用在你看来是无处定义的切换变量。

It looks like the toggle variable that you are trying to use is nowhere defined in your view.

在ASP.NET MVC控制器动作正常通过视图模型的意见。这些视图模型包含将由视图中使用属性。因此,在ASP.NET MVC应用程序一如既往通过定义视图模型启动:

In ASP.NET MVC controller actions normally pass view models to views. Those view models contain properties that will be used by the view. So as always in an ASP.NET MVC application you start by defining a view model:

public class MyViewModel
{
    public bool IsToggle { get; set; }
}

然后你写这本实例化视图模型,并将其传递到视图控制器动作:

then you write a controller action which instantiates this view model and passes it to the view:

public ActionResult Index()
{
    var model = new MyViewModel
    {
        IsToggle = true
    };
    return View(model);
}

最后你写一个强类型的视图这个视图模型,你可以使用它的属性。

and finally you write a strongly typed view to this view model where you can use its properties:

<%@ Page 
    Language="C#" 
    MasterPageFile="~/Views/Shared/Site.Master" 
    Inherits="System.Web.Mvc.ViewPage<AppName.Models.MyViewModel>" %>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <script type="text/javascript">
    (function () {
        debugger;
        var $a = 10;
        var $b = <%= Model.IsToggle %>;
        var $c = 12;
    });    
    </script>

</asp:Content>

现在让我们假设您的视图模型包含一些属性是字符串类型,甚至其他复杂的对象。在这种情况下,你需要正确的连接code他们。而最好的方法,以确保您正确编码要传递给JavaScript值是使用JavaScript文本(JSON对象):

Now let's suppose that your view model contains some properties that are of type string or even other complex objects. In this case you will need to properly encode them. And the best way to ensure that you are properly encoding values that you are passing to javascript is to use javascript literals (JSON notation):

<script type="text/javascript">
(function () {
    debugger;
    // we JSON serialize the entire model:
    var model = <%= new JavaScriptSerializer().Serialize(Model) %>;

    // Now you can manipulate individual model properties:
    alert(model.SomeProperty);
});    
</script>

请注意,<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx\"相对=nofollow>的JavaScriptSerializer 类是在 System.Web.Extensions程序装配中定义的 System.Web.Script.Serialization 命名空间,以便确保将这些成规模。

Note that the JavaScriptSerializer class is defined in the System.Web.Extensions assembly in the System.Web.Script.Serialization namespace so make sure to bring those into scope.

这篇关于获得一个MVC2可变进的Jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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