[英]adding js code with razor
I am generating some tabs dynamically. 我正在动态生成一些标签。 I end up with 4 tabs.
我最后有4个标签。 The class of the tabs is beeing set in the foreach loop, and it is set to be the languageId, as you can see in the code below.
选项卡的类是在foreach循环中设置的,并且设置为languageId,如下面的代码所示。
<div id="tabs">
<ul>
@for (int i = 0; i < Model.NoteViewModel.NoteTextViewModels.Count(); i++)
{
var languageId = @Model.NoteViewModel.NoteTextViewModels[i].LanguageId;
var imageName = string.Format("FlagSmall_{0}.gif", languageId);
<li>
<a href="#@languageId" class="@currentLanguage">
<img src="@Url.Content("~/Graphics/" + imageName)" />
</a>
</li>
}
</ul>
Now I introduce a new variable: var currentLanguage = new NotesController().LogonInfo.LanguageId;
现在,我引入一个新变量:
var currentLanguage = new NotesController().LogonInfo.LanguageId;
which holds the current language of the user logged in. What I want to do, is to set focus on the tab where href (or id or class) is the same as the currentLanguage. 它保留了登录用户的当前语言。我想做的是将焦点设置在href(或id或class)与currentLanguage相同的选项卡上。 For this I guess I need js, but since I can't have js code with razor syntax, I'm stuck.
为此,我想我需要js,但是由于我无法拥有带有剃刀语法的js代码,因此我陷入了困境。 So I want something like this:
所以我想要这样的事情:
@for (int i = 0; i < Model.NoteViewModel.NoteTextViewModels.Count(); i++)
{
var languageId = @Model.NoteViewModel.NoteTextViewModels[i].LanguageId;
var currentLanguage = new NotesController().LogonInfo.LanguageId;
var imageName = string.Format("FlagSmall_{0}.gif", languageId);
<li>
<a href="#@languageId">
<img src="@Url.Content("~/Graphics/" + imageName)" />
</a>
</li>
if (languageId == currentLanguage)
{
documentation.getElementById("currentLanguage").focus();
}
}
How can I achieve this? 我该如何实现?
I can't have js code with razor syntax
我无法使用带有razor语法的js代码
Sure you can, though you may need to explicitly tell the view engine that this is client-side content and not server-side code. 当然可以,尽管您可能需要明确告知视图引擎这是客户端内容,而不是服务器端代码。 You can do this with the
<text>
tag . 您可以使用
<text>
标记执行此操作。 It's not an HTML tag per se, but something the razor engine uses to specify static content. 它本身不是HTML标记,而是剃刀引擎用来指定静态内容的东西。 Something like this:
像这样:
<script type="text/javascript">
@for (int i = 0; i < Model.NoteViewModel.NoteTextViewModels.Count(); i++)
{
<text>
var someJavaScriptVariable = 'some value';
// etc.
</text>
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.