简体   繁体   English

C#中的JavaScript

[英]JavaScript inside C#

<script>
 ...

   var Klasa = calEvent.ClassID;
   @{
     var NameOfClass = Model.FirstOrDefault(xx => xx.ClassID == @:Klasa).Nazwa_Klasy;
   }

  $description.append($('<p/>').html('<b>Klasa:</b>' + @NameOfClass

 ...
</script>

How should I correct mix JS with C#? 如何正确将JS与C#混合使用?

I get information 'Cannot use local variable "NameOfClass" before it is declared', 我得到的信息是“在声明之前不能使用局部变量“ NameOfClass””,

but when I declare variable again before with string value then I get 'An opening "(" is missing the corresponding closing ")"' and then I am not sure what exactly I should do, because everything looks all right. 但是当我之前再次使用字符串值声明变量时,我得到“一个开头“(”缺少相应的结尾“)”',然后我不确定该怎么做,因为一切看起来都很好。

My main target is that I want to get single data from Model (Nazwa_Klasy) where ClassID is equal calEvent.ClassID. 我的主要目标是我想从ClassID等于calEvent.ClassID的Model(Nazwa_Klasy)中获取单个数据。 Maybe should I also change my expression? 也许我也应该改变我的表情?

Thanks for any answer! 感谢您的回答!

First of all, let's clarify few things: 首先,让我们澄清一些事情:

This will be var Klasa = calEvent.ClassID; 这将是var Klasa = calEvent.ClassID; happening on the client side. 发生在客户端。 Means, some action will cause calEvent in the browser. 意味着,某些操作将导致浏览器中的calEvent If yes , then it makes following pointless. 如果 ,则使后续操作毫无意义。

Model.FirstOrDefault(xx => xx.ClassID == @:Klasa)

As on the server (where this is run), klasa is not available. 与在服务器(运行该服务器的服务器)上一样, klasa不可用。 So your best bet is to spit Model , which seems to be an array on the client side and then filter that. 因此,最好的选择是吐出Model ,它似乎是客户端的一个数组,然后对其进行过滤。 Don't know if I'm making any sense, but do give following a try: 不知道我是否有意义,但请尝试以下操作:

<script>
    ...
    //Array of object to look into at client side
    var modelArray=@Html.Raw(JsonConvert.SerializeObject(Model));
    var Klasa = calEvent.ClassID;

    var NameOfClass=null;
    $.each(modelArray,function(){
        if($(this).ClassID===klasa){
            NameOfClass = $(this).Nazwa_Klasy;
            return;
        }
    });

    //You may want to test if `NameOfClass` has a value.
    $description.append($('<p/>').html('<b>Klasa:</b>' + NameOfClass
 ...
</script>

Also, as pointed out in comments, @:klasa is wrong. 另外,正如评论中指出的, @:klasa是错误的。 You don't need : there. 您不需要:在那里。

@TheVillageIdiot the way of Your thinking was good, but the code didn't work properly as I wanted. @TheVillageIdiot您的思维方式不错,但是代码没有按我想要的正常工作。 So, I changed it a little to 因此,我将其更改为

     var modelArray = @Html.Raw(JsonConvert.SerializeObject(Model));
                var Klasa = calEvent.ClassID;


                for (var i = 0; i < modelArray.length; i++) {
                    var obj = modelArray[i];
                    if (obj.ClassID === Klasa) {
                        NameOfClass = obj.Nazwa_Klasy.trim();
                    }
                }


                $description.append($('<p/>').html('<b>Klasa: </b>' + NameOfClass));

Right now everything is fine. 现在一切都很好。

Thanks a lot! 非常感谢! ;-) ;-)

This may not be the quick answer to your question, but there's a live demo addressing the issue of working with JavaScript inside ASP.NET MVC apps this week. 这可能不是您问题的快速答案,但是本周有一个现场演示解决了在ASP.NET MVC应用程序中使用JavaScript的问题。 It's on Oct 26 and I'm sure there will be a recording afterwards if you can't attend. 是10月26日,如果您不能参加,我确定以后还会有录音。 This is the registration page. 这是注册页面。 - you can also ask your questions there. -您也可以在此提问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM