Home > Software design >  Dynamically read of inputs Razor MVC C#
Dynamically read of inputs Razor MVC C#

Time:10-01

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.

CodePudding user response:

İ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

CodePudding user response:

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.

CodePudding user response:

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)">
  • Related