locked
How call js methode? RRS feed

  • Question

  • User2142405843 posted

    Hello friends!

    Please tell me how to call a function when you click on an element in the radiobutton list?

    I have list 

    controller
    ...
    foreach (var price in product.Prices)
                    {
                        if (inCartProduct.Price == price.Price)
                        {
                            flag = true;
                        }
                        viewModel.RBListModelPrice.Add(new RadioButtonListModelPrice()
                        {
                            IsChecked = flag,
                            Name = "price",
                            Value = String.Format("по {0} г - {1} ₽", price.Weight, price.Price.ToString()),
                            OnClick = String.Format("PriceChange({0})", price.Price.ToString()),
                            Price = price.Price,
                            Weight = price.Weight
                        });
                        flag = false;
                    }
    ...
    
    view
    @for (var i = 0; i < Model.RBListModelPrice.Count; i++)
                                {
                                    var attributes = Model.RBListModelPrice[i].IsChecked ? new { @checked = true } : null;
                                    var attributes2 = Model.RBListModelPrice[i].OnClick;
                                    <div>
                                        @Html.RadioButtonFor(p=> Model.Prices, Model.RBListModelPrice[i].IsChecked, new {@onClick="PriceChange(1)", @checked=Model.RBListModelPrice[i].IsChecked})
                                        <label>@Model.RBListModelPrice[i].Value</label>
                                    </div>                               
                                }
    
    @section Scripts {
        <script type="text/javascript">
            function PriceChange(price) {
                var summ = price * document.getElementById("quantity").value;
            }
        </script>
    }

    I need to call "PriceChange(price)" after click on an element in radio button list with parameter.

    Please tell me how to do it? 

    Monday, May 4, 2020 2:13 PM

Answers

  • User1686398519 posted

    Hi, Poul

    You can change the second parameter of “@ Html.RadioButtonFor“ to “Model.RBListModelPrice [i] .Price”. In addition, use "this.value" to pass the value of the currently selected RadioButton.

    @Html.RadioButtonFor(p => Model.Prices, Model.RBListModelPrice[i].Price, new { @onClick = "PriceChange(this.value)", @checked = Model.RBListModelPrice[i].IsChecked })
    
    

    Here is the result.

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 5, 2020 5:16 AM

All replies

  • User-474980206 posted

    just pass the calculated string

     @Html.RadioButtonFor(p=> Model.Prices, 
            Model.RBListModelPrice[i].IsChecked, 
            new {
               onClick=Model.RBListModelPrice[i].OnClick,
               checked=Model.RBListModelPrice[i].IsChecked  // will always be checked
            })

    to get the checked to work, bind to a bool:

    @Html.RadioButtonFor(p=> p.RBListModelPrice[i].IsChecked, 
            new {
               onClick=Model.RBListModelPrice[i].OnClick
            })
    

    note: you only need the @ prefix on attributes that are reserved words like class.

    Monday, May 4, 2020 3:28 PM
  • User1686398519 posted

    Hi, Poul

    You can change the second parameter of “@ Html.RadioButtonFor“ to “Model.RBListModelPrice [i] .Price”. In addition, use "this.value" to pass the value of the currently selected RadioButton.

    @Html.RadioButtonFor(p => Model.Prices, Model.RBListModelPrice[i].Price, new { @onClick = "PriceChange(this.value)", @checked = Model.RBListModelPrice[i].IsChecked })
    
    

    Here is the result.

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 5, 2020 5:16 AM