Solution 1 :

You can try to use a hidden input to save the variable.Here is a demo:

<div>
            @if (Model.EmployeeDetails != null)
            {
                <table id="EmpDetail" class="table table-bordered">
                    <thead>
                        <tr>
                            <th style="width: 30%">Name</th>
                            <th style="width: 70%">Age</th>
                        </tr>
                    </thead>
                    <tbody>
                        @{ string variable = "";}
                        @foreach (DTO.Employee emp in Model.EmployeeDetails)
                        {
                            <tr>
                                <td style="width: 30%">@emp.name</td> 
// Here I need to concatenate with comma separation to a variable 
and after the looping ends send to javascript function
                                <td style="width: 70%">@emp.age</td>
                            </tr>
                            if (variable != "")
                               variable += ",";
                            variable += @emp.name + "," + @emp.age;
                        }
                    </tbody>
                </table>
                <input id="details" hidden  value="@variable"/>
            }
        </div>
        <button onclick="getData()">getData</button>

js(when click button getData,function getData() will be called):

function getData() {
            alert($("#details").val());
        }

result:
enter image description here

Solution 2 :

Another approach would be to do the concatenation from your PageModel or Controller.

For example here is my EmployeesModel:

public class EmployeesModel : PageModel
{
    public string EmployeeCommaSeperatedValues { get; set; }
    public List<Employee> EmployeeDetails { get; set; }

    public EmployeesModel()
    {
        EmployeeDetails = new List<Employee>();
        EmployeeDetails.Add(new Employee { name = "John", age = 35 });
        EmployeeDetails.Add(new Employee { name = "Jane", age = 32 });
    }

    public void OnGet()
    {
        if (EmployeeDetails != null)
        {
            foreach (Employee emp in EmployeeDetails)
            {
                if (!string.IsNullOrEmpty(EmployeeCommaSeperatedValues))
                {
                    EmployeeCommaSeperatedValues += ",";
                }
                EmployeeCommaSeperatedValues += $"{emp.name},{emp.age}";
            }
        }
    }
}

I’ve added the property EmployeeCommaSeperatedValues to the model. In the OnGet method I run the foreach and store the value in EmployeeCommaSeperatedValues.

Since this property is in my PageModel I can access it from the view.

@page
@model dltLater.Pages.EmployeesModel
@using dltLater.Models
@{
}

<div>
    @if (Model.EmployeeDetails != null)
    {
        <table id="EmpDetail" class="table table-bordered">
            <thead>
                <tr>
                    <th style="width: 30%">Name</th>
                    <th style="width: 70%">Age</th>
                </tr>
            </thead>
            <tbody>
                @foreach (Employee emp in Model.EmployeeDetails)
                {
                    <tr>
                        <td style="width: 30%">@emp.name</td>
                        <td style="width: 70%">@emp.age</td>
                    </tr>
                }
            </tbody>
        </table>
    }
</div>

<script type="text/javascript">
    function jsFunctionCall(commaSeperatedValues) {
        console.log(commaSeperatedValues);
    }

    jsFunctionCall("@Model.EmployeeCommaSeperatedValues");
</script>

Problem :

I am using ASP.Net core with razor pages.

I have a table in html, and using razor syntax looping the class object in this HTML table, as code below.

Now I want to get the values while looping the table, save / concatenate with comma separated the values through the looping and send it as a variable to a JavaScript function. This save/ concatenate and send it as a variable to a JavaScript function part I am not sure how to do. Any help would be grateful.

 <div>
            @if (Model.EmployeeDetails != null)
            {
                <table id="EmpDetail" class="table table-bordered">
                    <thead>
                        <tr>
                            <th style="width: 30%">Name</th>
                            <th style="width: 70%">Age</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (DTO.Employee emp in Model.EmployeeDetails)
                        {
                            <tr>
                                <td style="width: 30%">@emp.name</td> 
// Here I need to concatenate with comma separation to a variable 
and after the looping ends send to javascript function
                                <td style="width: 70%">@emp.age</td>
                            </tr>
                        }
                    </tbody>
                </table>
            }
        </div>

<script type="text/javascript">jsFunctionCall("variableCommaSeperatedValues");</script> // here is the JavaScript 
function call with paramter conatianing comma separated values from above 
``````````

By