Question

I am doing an application in asp.net mvc. I have a radiobuttongroup to be set as Yes always and I want to disable it. This is my radiobutton group..

@Html.RadioButton("rdoGp_PriTenant", "rdoGp_PriTenantYes", true)<label>Yes</label>
@Html.RadioButton("rdoGp_PriTenant", "rdoGp_PriTenantNo", false)<label>No</label>

I tried

document.getElementById("rdoGp_PriTenant").disabled = true;

But it didnt work.Can anyone pls help..??

Was it helpful?

Solution

Lets take a look in MSDN: InputExtensions.RadioButton Method (HtmlHelper, String, Object, Object)

There is a constructor:

public static MvcHtmlString RadioButton(
    this HtmlHelper htmlHelper,
    string name,
    Object value,
    Object htmlAttributes
)

You have property htmlAttributes, where you could put your advanced HTML attributes (for example new { disabled = "disabled" }):

@Html.RadioButton("rdoGp_PriTenant", "rdoGp_PriTenantYes", true, new { disabled = "disabled" })<label>Yes</label>
@Html.RadioButton("rdoGp_PriTenant", "rdoGp_PriTenantNo", true, new { disabled = "disabled" })<label>No</label>

OTHER TIPS

when you use

 $("#rdoGp_PriTenant")

it selects only the first element with the given ID.

However, when you select by attribute (e.g. id in your case), it returns all matching elements, like so:

 $("[id=rdoGp_PriTenant]").attr('disabled', 'disabled');

Working code using Jquery would be:

 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>
    $(document).ready(function () {

            $("[id=rdoGp_PriTenant]").attr('disabled', 'disabled');

    });
</script>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top