1

Who can help me. When loading data to a mvc view, i'll load the value of the checkbox, but if true, i'll need to disable 2 textbox fields.

Code is a piece of my for loop

@Html.TextBoxFor(m => m.Items[i].StartIntervalTime, "{0:HH:mm}", new {                   
               Value = Model.Items[i].Available ? "" : Model.Items[i].StartIntervalTime.ToString("HH:mm"),
               id = "startTime" + i                                      
})
<span>untill</span>
@Html.TextBoxFor(m => m.Items[i].EndIntervalTime, "{0:HH:mm}", new {
               Value = Model.Items[i].Available ? "" : Model.Items[i].EndIntervalTime.ToString("HH:mm"),
               id = "endTime" + i
})
@Html.CheckBoxFor(m => m.Items[i].Available, new { id = i, @class = "closedallday" })

I've tried, in the textbox tag, with "disabled" like i did with "value", but disabled will always disables the textbox even without a value. So..

Disabled = Model.Items[i].Available ? "disabled" : "",

I need a real working solution.

Thank you Dinand

2
  • How does the html look like when you use Disabled = Model.Items[i].Available ? "disabled" : "",? Have you tried with lowercase disabled field instead? Commented Dec 24, 2014 at 8:03
  • why tag it with jQuery? Commented Dec 24, 2014 at 8:06

3 Answers 3

2

You can make something like this:

//Check if the value is true, draw the textbox disabled , otherwise draw it normally:

  if(m.Items[i].Availabl == true)
    {
     @Html.TextBoxFor(m => m.Items[i].StartIntervalTime, "{0:HH:mm}"
    , new {ANYTHINK ELSE, disabled="disabled" })
    }
    else
    {
    @Html.TextBoxFor(m => m.Items[i].StartIntervalTime, "{0:HH:mm}"
    , new {ANYTHINK ELSE })
    }
Sign up to request clarification or add additional context in comments.

Comments

1

I would use jQuery to catch the change in the checkbox then alter your text boxes. Here is a working jsfiddle

jQuery

$('#checkbox').on("change", function () {
    if ($(this).is(":checked")) {
        textbox.addClass('disabled');
        textbox.val("");
        textbox.prop('disabled',true);
    }else {
        textbox.removeClass('disabled');
        textbox.prop('disabled',false);
    }
}

CSS

.disabled {
    background-color:lightgray;
}

1 Comment

Thank you Yoetinger.. Data Loads after Jquery. So the script won't fire when needed..
0

Simply make it with jquery

<script>
    jQuery(document).ready(function() {
        if ($("input:checkbox").is(":checked")) 
        {               
             $("input:text").prop("readonly", "readonly");
        }
    });
</script>

2 Comments

Thanx Younis, Jquery is loaded and after that the data.. So the script will not fire after data load..
try instead jQuery(document).ready(function() write a function when data is loaded then the function execute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.