简体   繁体   中英

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

I'm trying to assign ac# variable from a javascript variable in ASP.Net MVC. But I get "CS1002:; expected" error.

  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#

Other options:

  • Store variable in cookie using js and access if from c#
  • Post request to server using AJAX call/form submission etc..

The C# code is server-side code which means it runs on the server. While JavaScript code runs in the browser.

The whole cycle goes like this:

Request comes in --> All C# code gets executed --> The rendered result is sent back

So that means, you can do something like this (inside .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:

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#; 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:

public IActionResult Joinings(int id) {
}

Now, the id parameter will get the value sent in the query string. 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. Alternatively, you can send the data in the request body (eg using POST or PUT ). Hope that helps.

Why don't get this value from a hidden field? like 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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