【问题标题】:ASP.Net core razor - looping through HTML table, how to comma separate the values and sent to JavaScript functionASP.Net core razor - 循环遍历 HTML 表,如何用逗号分隔值并发送到 JavaScript 函数
【发布时间】:2021-09-18 14:16:29
【问题描述】:

我正在使用带有剃须刀页面的 ASP.Net 核心。

我有一个 html 表格,并使用 razor 语法循环这个 HTML 表格中的类对象,如下代码。

现在我想在循环表格时获取值,保存/用逗号连接通过循环分隔值并将其作为变量发送到 JavaScript 函数。此保存/连接并将其作为变量发送到 JavaScript 函数部分,我不知道该怎么做。任何帮助将不胜感激。

 <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 
``````````

【问题讨论】:

    标签: javascript html asp.net-core razor


    【解决方案1】:

    您可以尝试使用隐藏输入来保存变量。这里有一个演示:

    <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(当点击按钮getData,function getData()会被调用):

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

    结果:

    【讨论】:

      【解决方案2】:

      另一种方法是从您的 PageModel 或 Controller 进行连接。

      例如这里是我的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}";
                  }
              }
          }
      }
      

      我已将属性EmployeeCommaSeperatedValues 添加到模型中。在OnGet 方法中,我运行foreach 并将值存储在EmployeeCommaSeperatedValues 中。

      由于这个属性在我的 PageModel 中,我可以从视图中访问它。

      @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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-13
        • 2021-02-24
        • 1970-01-01
        • 1970-01-01
        • 2012-05-28
        • 1970-01-01
        • 1970-01-01
        • 2014-10-26
        相关资源
        最近更新 更多