I have the below code in CSHTML but seems like the dropdown is not getting disabled
#Html.DropDownListFor(x => x.Task_Status_Code, Model.TaskStatus, new { #class = "form-control", #disabled = "disabled" })
I want to disable it but it is not working.
#Html.DropDownListFor(
x => x.Task_Status_Code,
Model.TaskStatus,
new { #class = "form-control", disabled = "disabled" })
Removed the # sign before the disabled property name.
You need to add the # character before any dotnet keywords like class. Custom properties do not need to have # prefix.
Please check with below snippet !
#Html.DropDownListFor(
x => x.ReminderTime,
Model.RemindersList,
new { disabled = "disabled" }
)
#Html.DropDownListFor(x => x.Task_Status_Code, Model.TaskStatus, new { disabled = "disabled" })
That will work as well unless you define css form control with in then
#Html.DropDownListFor(x => x.Task_Status_Code, Model.TaskStatus, new { #class="form-control", disabled = "disabled" })
Remember not to put #sign before disabled
Related
Both of these codes have the same result in the final markup. so why is the # used in the htmlAttributes section?
#Html.TextBoxFor(model => model.ManagerName, new { #autocomplete = "off", #maxlength = "40" })
#Html.TextBoxFor(model => model.ManagerName, new { autocomplete = "off", maxlength = "40" })
Does the MVC version have an effect on this?
It may have been mandatory in older versions!
In the "new { ... }" block, some words such as "class" are keywords. # is required in order to escape the keyword. For non-keywords, it doesn't make a difference.
Hi I need to do the following:
#Html.TextBoxFor(m => m.Login,
new {
#class = "form-control",
#placeholder = "Username",
required="true" data-required-message="Please insert your name"
})
But Im getting error on data-required-message seems that I can't use "-".
Any clue?
You have to use it with underscore _ and razor will render it as - in html:
data_required_message="Please insert your name"
another thing to note is that you don't need to put # sign in front of every parameter of htmlAttributes , we put it for class becasue class is a reserved word in c#, so it cannot be used directly as a variable name.
your final code will be like:
#Html.TextBoxFor(m => m.Login,
htmlAttributes: new {
#class = "form-control",
placeholder = "Username",
required="true",
data_required_message="Please insert your name"
})
you are also missing a comma after placeholder attribute.
Try data_required_message instead of data-required-message
I want the disabled attribute be added to a textbox based on a condition
#Html.TextBoxFor(m => m.kidsNumber, new { (Model.kidsDropDown != "2") ? "#disabled" : ""})
Use
#Html.TextBoxFor(m => m.kidsNumber, Model.kidsDropDown != "2" ? new {disabled = "disabled"} : null )
Note also if you need to add multiple attributes, then it needs to be in the format (where the attributes are cast to object
#Html.TextBoxFor(m => m.kidsNumber, Model.kidsDropDown != "2" ? (object)new { #disabled = "disabled", #class="form-control" } : (object)new { #class="form-control" })
If you have multiple textboxes that use the same sets of attributes, you can assign these to variables in the view
#{
object number = new { #type = "number", #class="form-control" };
object disabledNumber = new { #disabled = "disabled", #class="form-control" };
}
and in the form
#Html.TextBoxFor(m => m.kidsNumber, Model.kidsDropDown != "2" ? disabledNumber : number)
#Html.TextBoxFor(m => m.anotherProperty, AnotherCondition ? disabledNumber : number)
.....
You have not said which attribute you want to set ::
you code is without attribute and you should use
use readonly instead because disabled fields are not posted on form submit
#Html.TextBoxFor(m => m.kidsNumber, new { (Model.kidsDropDown != "2") ? "#disabled" : ""})
For Disabled attribute Use::
#Html.TextBoxFor(m => m.kidsNumber, Model.kidsDropDown != "2" ? new {disabled = "disabled"} : null )
For readonly Use something like::
#Html.TextBoxFor(m => m.kidsNumber, new { #readonly= (Model.kidsDropDown != "2" ? "readonly" : "")})
This is what I tried:
#Html.TextBoxFor(model => model.EntryDate, new { style="width:90px;",((_new || _modify) ? #disabled="disabled":"") }) #Html.ValidationMessageFor(model => model.EntryDate)
The following will work because there is only one html attribute
#Html.TextBoxFor(m => m.EntryDate, (_new || _modify)
? new { #disabled = "disabled" }
: null)
However once you add the style attribute, this will generate an error because there is no implicit conversion between new { #disabled = "disabled", #Style="width:90px;" } and new { #Style="width:90px;" } so you need to do this in an if block
#if(_new || _modify) {
#Html.TextBoxFor(m => m.EntryDate, new { #disabled = "disabled", style="width:90px;")}
} else {
#Html.TextBoxFor(m => m.EntryDate, new { style="width:90px;" })
}
Edit
Casting the html dictionary to object seems to work but I've not fully tested this (and I'm not sure that its "a simple and elegant way to to it")
#Html.TextBoxFor(m => m.EntryDate, (_new || _modify)
? (object)new { #disabled = "disabled", style="width:90px;" }
: (object)new { style="width:90px;" })
I am not sure but try this one:
#Html.TextBoxFor(model => model.EntryDate, (_new || _modify) ? new{ #Style="width:90px;", #Disabled="disabled"} : new{ #Style="width:90px;"})
#Html.DropDownListFor(m => m.Baled, new SelectList(new [] {"Select Type","Option1", "Option2", "Option3"}))
I want to add a css class in the dropdown list.
I have tried this
#Html.DropDownListFor(m => m.Baled, new SelectList(new [] {"Select Type","Option1", "Option2", "Option3"}, new {#class="myclass"}))
But it doesn't work
If anyone can suggest me any other better way to display dropdown list will be ok too.
You have close the bracket at wrong place. you have defined attribute in second paramter of selectlist. actually it should be close first and define in the last parameter of Html.DropDownListFor
#Html.DropDownListFor(m => m.Baled, new SelectList(new[] { "Select Type", "Option1", "Option2", "Option3" }), new { #class = "myclass" })