Link to home
Start Free TrialLog in
Avatar of chokka
chokkaFlag for United States of America

asked on

How to invoke the JavaScript function from code behind (c# / controller)?

How to invoke the JavaScript function from code behind (c# / controller)?

In MVC, Controller - I will have to enable or disable the text box in the Controller.

In c#, i will have to fetch the HTML element id.

Then based on the condition result loaded from database, I will have to enable or disable the Textbox element id.

if ($("input:radio[name='RadioButtonSelection']:checked").length > 0) 
{
            var isChecked = document.getElementById("RadioButtonSelectionYes").checked;

            if (isChecked == true)
            {
                document.getElementById('ShowTextBox').disabled = false;
                
            } else if (isChecked == false)
            {
                document.getElementById('ShowTextBox').checked = true;
                document.getElementById('ShowTextBox').disabled = true;
            }
}

Open in new window

Avatar of Ioannis Paraskevopoulos
Ioannis Paraskevopoulos
Flag of Greece image

Hi,

If you have a razor view you can do that with razor.

For instance have a variable in ViewBag in your controller like this:

ViewBag.isDisabled = true;

Open in new window


Then in your view you may have it like this:

<input type="checkbox" disabled="@(ViewBag.isDisabled == true ? "disabled" : string.Empty)" />

Open in new window


Giannis
Avatar of chokka

ASKER

Thank you
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.