简体   繁体   English

c#来自javascript变量错误的变量分配(CS1002 :;预期)

[英]c # variable assignment from javascript variable error (CS1002:; expected)

I'm trying to assign ac# variable from a javascript variable in ASP.Net MVC. 我正在尝试从ASP.Net MVC中的javascript变量分配ac#变量。 But I get "CS1002:; expected" error. 但是我收到“ CS1002 :;预期”错误。

  function openEditJoining(joining_id) {
           @{ 
               int joiningId = @:joining_id ;
           }
  }

C# and JS are processed in different places so you can't assign a variable directly from JS to c# C#和JS在不同的地方处理,因此您不能直接将JS中的变量分配给c#

Other options: 其他选项:

  • Store variable in cookie using js and access if from c# 使用js将变量存储在cookie中,如果来自c#,则进行访问
  • Post request to server using AJAX call/form submission etc.. 使用AJAX调用/表单提交等将请求发布到服务器。

The C# code is server-side code which means it runs on the server. C#代码是服务器端代码,这意味着它在服务器上运行。 While JavaScript code runs in the browser. JavaScript代码在浏览器中运行。

The whole cycle goes like this: 整个周期如下:

Request comes in --> All C# code gets executed --> The rendered result is sent back 请求进入->所有C#代码都被执行->呈现的结果被发回

So that means, you can do something like this (inside .cshtml ): 因此,您可以执行以下操作(在.cshtml ):

function openEditJoining(joining_id) {
    return @Model.JoiningId;
}

The C# code will run first and let's say that JoiningId = 56 , the returned result would then be: C#代码将首先运行,并假设JoiningId = 56 ,然后返回的结果将是:

function openEditJoining(joining_id) {
    return "56";
}

This is perfectly valid because the server side code executes first. 这是完全有效的,因为首先执行服务器端代码。 However, you are trying the other way around, which is illegal. 但是,您正在尝试另一种方法,这是非法的。 Once the result is returned back to the client, it's not C#; 一旦结果返回给客户端,它就不是C#。 it's all client-side now. 现在都是客户端。

What you can do is you can send the variable in a request using a query string or request body. 您可以做的是,可以使用查询字符串或请求正文在请求中发送变量。 Eg send request as localhost:49976/[controllerName]/joinings?id=56 and inside that controller, change the action method's signature to: 例如,以localhost:49976/[controllerName]/joinings?id=56身份发送请求,并在该控制器内部,将操作方法​​的签名更改为:

public IActionResult Joinings(int id) {
}

Now, the id parameter will get the value sent in the query string. 现在, id参数将获取在查询字符串中发送的值。 And of course, the id can be named anything you want as long as the query string and the one defined in the method parameter are the same. 当然,只要查询字符串和method参数中定义的id就可以将id命名为任意名称。 Alternatively, you can send the data in the request body (eg using POST or PUT ). 或者,您可以在请求正文中发送数据(例如,使用POSTPUT )。 Hope that helps. 希望能有所帮助。

Why don't get this value from a hidden field? 为什么不从隐藏字段中获得此值? like https://jsfiddle.net/Alan_van_Buuren/7kg99kh3/ https://jsfiddle.net/Alan_van_Buuren/7kg99kh3/

  <div class="container">
      <h3>Why don't get this value from a hidden field? like</h3>
      <p>I see your question in: https://stackoverflow.com/questions/46040155/c-variable-assignment-from-javascript-variable-error-cs1002-expected</p>
      <div>
        <div class="form-group">
          <label>Your value :</label>
          <input type="text" id="field" class="form-control" />
          <input type="hidden" id="yourFieldInModel" class="form-control" />
        </div>
        <button class="btn btn-info">Send to model...</button>
      </div>
      <p id="valueHidden">The value is: </p>
    </div>

    <script>
    //Method that assign your frontValue into a field in model    
    function getSome(valueFromModel) {
      // TODO: anything
      $('#yourFieldInModel').attr('value', valueFRomModel);
    }

    $(document).ready(function() {
      $('#field').on('change', function() {
        var thisValue = $(this).val();
        $('#valueHidden').text('The value is ' + thisValue);
        getSome(thisValue);
      });
    });
</script>

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

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