简体   繁体   English

在Razor文件中使用C#代码和函数

[英]Using C# Code and Functions in Razor File

I have a problem with using functions in razor. 我在剃须刀中使用功能时遇到问题。 I have a list which my entities stored. 我有我的实体存储的列表。 I wrote a script but something wrong. 我写了一个脚本,但是出了点问题。 I have never done this before. 我以前从未做过。 Im not good at HTML or Jscript etc. 我不太擅长HTML或Jscript等。

MY CODE 我的密码

    @model IEnumerable<Contact>

    <script>

       var name = document.getElementById("nameBox").value;

       function nameBoxOperations(name) {
           @foreach (var names in @Model)
           {
               if (names.givenName == name)
               {
                   //OPERATIONS
               }
           }
       }

    </script>

In this code i want to check an input if it has stored in my list. 在此代码中,我想检查输入是否已存储在列表中。 But i cant reach "name" variable. 但我无法达到“名称”变量。 If i write this "@string name = document.getElementById("nameBox").value;" 如果我写这个“ @string name = document.getElementById(” nameBox“)。value;” it returns an error something i dont know. 它返回错误,我不知道。 How can i reach it, Thanks 我如何到达,谢谢

You can try this solution: 您可以尝试以下解决方案:

@model IEnumerable<Contact>

<script>
     var name = document.getElementById("nameBox").value;

     var collection = '@(Model.Select(x => x.givenName).Aggregate((a, b) => a + "," + b))'.split(',');        
     function nameBoxOperations(name) {
          for(var i = 0; i < collection.length; i++)           
               if (collection[i] == name)
               {
                   //OPERATIONS
               }           
          }
</script>

Razor runs on the server when the page is fetched. 提取页面后,Razor在服务器上运行。 It's primary purpose is to generate page content: 它的主要目的是生成页面内容:

@foreach (var names in @Model)
{
    <p>User name: @name</p>
}

If you inspect the network result from fetching the page, you'll see the result does not contain any Razor code. 如果检查获取页面的网络结果,您将看到结果不包含任何Razor代码。 This is because Razor runs entirely on the server, and only its results appear to the client: 这是因为Razor完全在服务器上运行,并且只有其结果显示在客户端上:

<p>User name: Bob</p>
<p>User name: Jimmy</p>
<p>User name: Glover</p>

No razor makes it to the client. 没有剃须刀能打给客户。

You appear to expect the Razor code to run on the client side as part of the Javascript. 您似乎希望Razor代码作为Javascript的一部分在客户端运行。 Razor is not a client side language. Razor不是客户端语言。 So Razor will not be able to evaluate a client side javascript parameter name . 因此Razor将无法评估客户端javascript参数name From Razor's perspective, the declaration of the JavaScript function and parameter are just content. 从Razor的角度来看,JavaScript函数和参数的声明只是内容。

If you want to be able to loop over the names in the ViewModel from javascript, then you can convert the viewmodel into a JSON object: 如果您希望能够从javascript循环遍历ViewModel中的名称,则可以将viewmodel转换为JSON对象:

var nameListJson = @(Html.Raw(Json.Encode(Model)));

function nameBoxOperations(name) 
{
   for (i = 0; i < nameListJson.length; i++) { 
       console.log( nameListJson[i] );
   }
}

I've shown a loop, but whatever you need to do, the data from the model is now accessible. 我已经显示了一个循环,但是无论您需要做什么,现在都可以访问模型中的数据。 You need to only use javascript though when looping and accessing the model. 尽管在循环和访问模型时,您只需要使用javascript。

If you view the content of the page retrieved, you'll see no Razor. 如果查看检索到的页面的内容,则不会看到Razor。 Instead you'll see an inline JSON object. 相反,您将看到一个内联JSON对象。

That is a pretty unconventional approach however. 但是,这是一种非常常规的方法。 Depending on your application, there's probably a better approach. 根据您的应用程序,可能有更好的方法。 Based on your question, it sounds like you probably should spend more time in tutorials and examples to see what are more common practices. 根据您的问题,听起来您可能应该花更多的时间在教程和示例上,以了解更常见的做法。 These will give you a better mental picture of how MVC works, and some building blocks of knowledge to compose your application from. 这些将使您对MVC的工作方式有更清晰的了解,并从中获得一些知识来构建您的应用程序。

While Using Javascript inside a C# code block ,you should use @: at the start of each line. 在C#代码块中使用Javascript时,应在每行的开头使用@:。

You may use this code: 您可以使用以下代码:

@model IEnumerable<Contact>

<script>

   var name = document.getElementById("nameBox").value;

   function nameBoxOperations(name) {
       @foreach (var names in @Model)
       {
           @:if (names.givenName == name)
           {
               @:alert('name matched');
               //OPERATIONS
           }
       }
   }

</script>

You're mixing JS with C#, which is not done. 您正在将JS与C#混合使用,但还没有完成。 Error must happen. 必须发生错误。 The simple solution is for you to type in more JS code which use Ajax (eg via jQuery, which is quite more straightforward than JS' Ajax coding) to fetch the data into another JS variable like an Array or something. 一个简单的解决方案是让您键入更多使用Ajax的JS代码(例如,通过jQuery,这比JS的Ajax编码更直接)将数据提取到另一个JS变量(例如Array或类似的东西)中。 Then, you can loop through that. 然后,您可以循环浏览。 You will have to learn some jQuery to do that though. 您将必须学习一些jQuery来做到这一点。

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

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