当前位置: 动力学知识库 > 问答 > 编程问答 >

asp.net mvc - MVC and Generic Edit Functionality using jQuery

问题描述:

Is it possible to create a js object and a few functions in a razor partial that can be shared among derived partials?

I want to do something like....

_BasePartial

{

define something using js

}

...

_DerivedPartial:_BasePartial

{

update something js

cause _base to act on something js

}

网友答案:

The JS defined both in the BasePartial and DerivedPartial views end up executing on the same page context, so there should be no problem with your use case. For example:

_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(); 
分享给朋友:
您可能感兴趣的文章:
随机阅读: