Pass C# Model from View to Javascript

前端 未结 4 1665
耶瑟儿~
耶瑟儿~ 2021-01-24 12:36

I\'m passing this ViewModel to my View:

  public class DashboardViewModel
  {
    public List UserTasks {get; set;}

    public List

        
4条回答
  •  不要未来只要你来
    2021-01-24 12:42

    You should store the data you use to loop to a javascript variable and query that when your button is clicked. Keep a unique identifier on your button' data attribute which you can use to query later. Since it is an array, you can simply use a counter variable which matches with the (js) array index.

    @{ var counter = 0;}
    @foreach (var item in Model.WorkItems)
     {
        @item.Name
        
        counter++;
     }
    

    and in the same razor view, have this javascript where you will serialize the Model.WorkItems collection to a javascript array and store it in a variable.

    
    

提交回复
热议问题