使用jQuery的MVC和通用编辑功能
是否可以在razor partial中创建一个js对象和几个可以在派生partial之间共享的函数 我想做一些像使用jQuery的MVC和通用编辑功能,jquery,asp.net-mvc,razor,kendo-ui,Jquery,Asp.net Mvc,Razor,Kendo Ui,是否可以在razor partial中创建一个js对象和几个可以在派生partial之间共享的函数 我想做一些像 _BasePartial { define something using js } ... _DerivedPartial:_BasePartial { update something js cause _base to act on something js } BasePartial和DerivedPartial视图中定义的JS最终在同一页面上下文中
_BasePartial
{
define something using js
}
...
_DerivedPartial:_BasePartial
{
update something js
cause _base to act on something js
}
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(函数(){var something.someVar=4;并调用doSeomthing();}在派生类中,我继续得到someVar值的2。派生脚本低于基准。我不知道我做错了什么。“var something.someVar=4;”不是有效的JS语句。无论如何,如果要访问全局范围变量,不应该使用“var”,因为使用“var”只会用s来声明局部变量ame名称,有效地将全局名称隐藏在其范围内。