0

Hi i got to use c# code in js. My js and .cshtml page are separeted . I wrote a function in .cshtml page and gotta call it in .js file.

In Js file

DataList.forEach(function (item) {
     ...
 var users = GetUsers(item.AssignedUsers);

It goes here

Index.cshtml

  <script>
    
        function GetUsers(userIdList) {
    //it logs it, i can get theese values from here
            console.log("userIdList");
            console.log(userIdList);
              @{ // but in here it says userIdList does not exist in current content
//also tryed this.userIdList
                  var users = userIdList.Split(",");
                  foreach (var item in users)
                  {
                      var user = _Manager.FindByIdAsync(item.UserId).Result;
    
                  }
              }
    
          }
    </script>
9
  • Anyone to help me, i can't reach the userIdList function parameter in c# razor Commented Dec 30, 2020 at 7:46
  • in here it says userIdList does not exist ...yes because it's a JavaScript variable not a c# variable. You can't just mix the languages together like that. They are executed at different times and in different places (browser for JavaScript, server for c#). If you need to invoke some c# based on values generated by JavaScript then the obvious thing to do would be to make an AJAX request to the server which can then execute the c# and return a response Commented Dec 30, 2020 at 7:49
  • It might help you to read this: stackoverflow.com/questions/13840429/… (a lot of the examples are about PHP but the same concepts apply to asp.net or any other server side language) Commented Dec 30, 2020 at 7:55
  • I understand, i also thought ajax but it may cost much.It's a solution but i gotta set simple data. Also i'm looping it Commented Dec 30, 2020 at 7:57
  • You can send the whole list to the server in one Ajax request and loop it there, and then return a list in the response. No need for multiple Ajax requests which would be inefficient Commented Dec 30, 2020 at 8:04

1 Answer 1

1

You can pass list from ajax to controller action,and action return a list you want. Here is a demo:

<button onclick="sendData()">
    send
</button>
<script>
function sendData() {
                //here is a sample list
                var list = [];
                var user1 = {};
                user1.Id = 1;
                user1.Name = "u1";
                list.push(user1);
                var user2 = {};
                user2.Id = 2;
                user2.Name = "u2";
                list.push(user2);
                $.ajax({
                type: "POST",
                url: '@Url.Action("SendData", "Test")',
                contentType: "application/json",
                data: JSON.stringify(list),
                }).done(function (data) {
                    console.log(data);
                     //the data is you want
                });
            }
</script>

action:

public List<User> SendData([FromBody]List<User> list) {
            //you can do something and return a list you want here
            return list;
        }

User:

public class User {
        public int Id { get; set; }
        public string Name { get; set; }

    }

result: enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.