0

I'm trying to call "demo" function from @onchange event, The function is declared inside @functions{} section. When the event is triggered it says "demo" is not defined.

Is anyone have any idea ?

MyCode:

@Html.DropDownList("TransactionType",
    new SelectList(Enum.GetValues(typeof(Forex.Reports.ReportValueType))),
    "Transaction Type",
    new
    {
        @class = "form-control",
        @id = "type",
        @onchange = "@demo(GetDropDownValue('type'))"
    }
)


@functions{

    public void demo (string selectedValue)
    {

        IEnumerable<Mt4Transaction>
            results =
            from result in Model
            select result;

        switch (selectedValue)
        {

            case "Withdrawal":
                {
                    results =
                    from result in Model
                    where result.Profit > 0
                    select result;
                }
                break;
            case "Deposit":
                {
                    results =
                    from result in Model
                    where result.Profit < 0
                    select result;
                }
                break;
            case "UnidentifiedProfit":
                {
                    results =
                    from result in Model
                    select result;
                }
                break;
        }


        Html.RenderPartial("MonetaryTransactionsPartial", results);

    }

Thanks, Yaniv

  • 1
    `public void demo()` is server side code. `@onchange = "@demo()` is client side code. You cannot run server side code in the browser unless you use ajax to call a server method (or redirect to a method) –  Mar 15 '17 at 09:18
  • And as a side note, do not execute code like that in the `.change()` event of a ` –  Mar 15 '17 at 09:30
  • I got a partial view which i render each time i get the result, Can you write an example for the ajax call ? – Yaniv Jacob Jacob Mar 15 '17 at 09:37
  • I know :) - You need to use ajax to call a server method (passing the value of the selected options) which returns the partial view and then update the DOM. Refer [this answer](http://stackoverflow.com/questions/29142422/rendering-partial-view-on-button-click-in-asp-net-mvc/29142790#29142790) for an example –  Mar 15 '17 at 09:40
  • why I can't call it from onchange ? this is the behaviour I need... – Yaniv Jacob Jacob Mar 15 '17 at 09:44
  • You can (if you want to annoy your users and degrade performance) –  Mar 15 '17 at 09:52

0 Answers0