Secane
Secane

Reputation: 41

Dynamically read of inputs Razor MVC C#

I have problem with aggregating inputs from dynamically created combo boxes. In my view .cshtml I'm creating multiple elements with multiple choices as and I'm trying to set onClick trigger to this element like in snippet:

 @foreach (var value in values)
  {
     <option onclick="@UpdateSelectedFilters(item.DisplayName, value)" value="@value" >@value</option>
  }

But unfortunately this function "@UpdateSelectedFilters()" is executed on render of element, not on click event like i want to.

Upvotes: 0

Views: 87

Answers (3)

Secane
Secane

Reputation: 41

Changing to javascript wasn't enough, model should be binded too in for example in asp-items

<select id="@name" class="form-control col-lg-3" style="margin-right: 10px; margin-top:10px" name="@name" asp-items="@options" onchange="UpdateSelectedFilters(this.name,this.value)">

Upvotes: 0

Yiyi You
Yiyi You

Reputation: 18159

UpdateSelectedFilters is a js function,so you don't need to use @ before it.Try to use

<option onclick="UpdateSelectedFilters(@item.DisplayName, @value)" value="@value" >@value</option>

so that you can pass C# data to js function.

Upvotes: 1

cemil
cemil

Reputation: 71

İf your project not Blazor You cant use @ at onClick event.

you can create a javascript function like:

function UpdateSelectedFilters(displayname, value){
    ...some code 
}

You can check the answer below

here

Upvotes: 1

Related Questions