0

I have this mvc razor code where on click of button I am populating a dropdown ClinicIDSelect. Here I am comparing the selected value of another dropdown InvoiceSelect and data from Model and populating the ClinicIDSelect dropdown with filtered value.

<select class="form-control" id="ClinicIDSelect">
  @foreach (DataRow r in Model.Tables["Clinic"].Rows)
  { 
    var inv_nbr = @r["INVOICE_NBR"].ToString();                         
    if ("inv_nbr" == $('#InvoiceSelect').val()) { 
      <option value="@r["CLINIC_CODE"]">@r["CLINIC_CODE"]</option> 
    }
  }
</select>

The issue is - it seems that I cannot compare the inv_nbr(C# string) with a jquery value. It shows a small red squiggly underline in jquery .val() saying that $ unexpected character, invalid expression, ; expected, } expected.

What could be the options to achieve this ?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Akshay
  • 1,412
  • 2
  • 17
  • 51
  • You can't do it that way; the Razor code is basically some C# code that executes server-side, and it just renders (generates) the static HTML content, etc., *before* any of it even gets to the client's browser. You are trying to execute jQuery in C#. What you can do is create a JavaScript function (say, an event handler) and inject bit's of Razor code into that - but that's not ideal. See [this](https://stackoverflow.com/a/4599403/8166486) for more detail, and alternatives. – Filip Milovanović May 11 '18 at 13:56
  • @FilipMilovanovićThanks, will look for other alternatives. – Akshay May 11 '18 at 14:23

0 Answers0