【问题标题】:MVC and Generic Edit Functionality using jQuery使用 jQuery 的 MVC 和通用编辑功能
【发布时间】:2012-11-17 02:38:26
【问题描述】:

是否可以在一个 razor 部分中创建一个 js 对象和一些函数,以便在派生的部分之间共享?

我想做类似的事情......

_BasePartial
{
   define something using js
}

...

_DerivedPartial:_BasePartial
{
   update something js
   cause _base to act on something js
} 

【问题讨论】:

    标签: jquery asp.net-mvc razor kendo-ui


    【解决方案1】:

    在 BasePartial 和 DerivedPartial 视图中定义的 JS 最终会在相同的页面上下文中执行,因此您的用例应该没有问题。例如:

    _BasePartial:
        var something = { someVar: 2 };
    
        function doSomething() {
            printSomething(); //Defined on the derived view
        }
    
    _DerivedPartial :
        function updateSomething() {
            something.someVar = 4;
    
            doSomething(); //Defined on the base view, should output "4" on the console
        }
    
        function printSomething() {
            console.log(something.someVar);
        }
    
    SomewhereInThePage :
        //Just make sure that the BasePartial JS was executed when calling the 
        // function defined on the derived view
        updateSomething(); 
    

    【讨论】:

    • 谢谢!我正在寻找确保页面上只有一份脚本副本的方法。在任何时候页面上可能存在一个或多个派生视图实例的情况下。
    • ...对不起嗨,当我在派生类中设置 $(document).ready(function (){var something.someVar=4; 并调用 doSeomthing();} 时,我继续得到2 为 someVar 的值。派生脚本在基数之下。我不知道我做错了什么。
    • 'var something.someVar = 4;'不是有效的 JS 语句。无论如何,如果要访问全局范围变量,则不应使用“var”,因为使用“var”只会声明一个具有相同名称的局部变量,从而有效地将全局变量隐藏在其范围内。
    猜你喜欢
    • 2011-04-29
    • 2012-08-10
    • 1970-01-01
    • 1970-01-01
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-07
    相关资源
    最近更新 更多