user616076 user616076 - 5 months ago 10
HTML Question

How can I create my @Html.TextBoxFor() as read only or diabled

I have a view which creates components dynamically in a partial view and in some cases these components need to be read only. I've managed it successfully with other components using the code below

$(document).ready(function () {
if("@Model.IsUnderReview" == "True") {
document.getElementById("textbox_" + "@Model.ID").disabled = true;
}
});


However in this case they are created in a loop and there could be any number of them. How do I take the code below and set it as read only?

@Html.TextBoxFor(m => m.Configuration.First(c => c.ID == config.ID && c.ProductRow == config.ProductRow).TextboxEntry, new Dictionary<string, object>{
{"id", "infoTextbox_" + Model.NumberOfDuplicates + "_" + config.ID},
{"configid", (config.ID + Model.NumberOfDuplicates)},
{"onchange", "HandleChange($(this))"}})


I've tried inserting @readonly = "readonly" but I either got the syntax wrong as it didn't work.

Answer

If you just want to add it in the htmlAttributes:

            @Html.TextBoxFor(m => m.Configuration.First(c => c.ID == config.ID && c.ProductRow == config.ProductRow).TextboxEntry, new Dictionary<string, object>{
                    {"id", "infoTextbox_" + Model.NumberOfDuplicates + "_" + config.ID},
                    {"configid", (config.ID + Model.NumberOfDuplicates)},
                    {"onchange", "HandleChange($(this))"}}, new { @disabled = "disabled"})