Conditionally disable textbox in Razor view - asp.net-mvc

Its a quick question.
#Html.TextBoxFor(model => model.VIN, string.IsNullOrEmpty(Model.VIN) ? new { #class = "required Vin" } : new { #disabled = "disabled" })
I get the error that Type of expression can not be determined because there is no implicit conversion anonymous type #1 and anonymous type #2.
Is there a way to conditionally disable text box?

Try something like
#Html.TextBoxFor(model => model.VIN, string.IsNullOrEmpty(Model.VIN) ? new { #class = "required Vin" } : (object)new { disabled = "disabled" })

Related

MVC 5 TextBoxFor not able to do conditional Bootstrap CSS?

I'm using a TextBoxFor and what I need to do is see if a property is NULL or Blank. If so then I need to let the end user type in their info. If not, meaning I can find their name, I need to disable the field so that they can not alter it.
I did some searching and try these two options but they are not working.
#Html.TextBoxFor(m => m.EmployeeName, string.IsNullOrWhiteSpace(Model.EmployeeName) ? new { Class = "form-control" } : new { Class = "form-control", disabled = "disabled" })
#Html.TextBoxFor(m => m.EmployeeName, new { Class = "form-control", string.IsNullOrWhiteSpace(Model.EmployeeName) ? disabled = "disabled" : null })
Any ideas on what I can try?
Your current code would be giving an error because there is no implicit conversion between the 2 anonymous types and you would need to cast them to object
var attributes = String.IsNullOrWhiteSpace(Model.EmployeeName) ?
(object)new { #class = "form-control" } :
(object)new { #class = "form-control", disabled = "disabled" })
#Html.TextBoxFor(m => m.EmployeeName, attributes)
Note however that disabling an control means its value will not be submitted, so if EmployeeName initially has a value, it will be null when submitted. You might want to consider using readonly = "readonly" instead.

How do I disable my #Html.ListBoxFor()

I need to be able to disable this ListBoxFor() depending on the Review status. Although the code below works it shows all list items.
#Html.ListBoxFor(m => m.ListOptions, filetypes, Model.IsUnderReview ? new { #class = "disabled" } : new { #class = "multiselectFileTypes" })
What would be the syntax to disable it but just so it shows '2 items selected'?
You cannot do inline condition checking for the htmlAttributes parameter.
This should work.
#using (Html.BeginForm())
{
<label>Select something </label>
if (!Model.IsUnderReview )
{
#Html.ListBoxFor(m => m.ListOptions, filetypes, new { #class = "multiselectFileTypes"})
}
else
{
#Html.ListBoxFor(m => m.ListOptions, filetypes, new { disabled = "disabled" })
}
}
It seems the answer is to use the Html.helper command HiddenFor(), like this
#if (Model.IsUnderReview)
{
//You then need to generate a hidden input for each value in SelectedRoles, as it's an array
#Html.HiddenFor(m => m.SelectedRoles)
}
else
{
#Html.ListBoxFor(x => x.SelectedRoles, filetypes, new { #class = "multiselectFileTypes" , id = "staticFieldM_" + Model.ID})
}
By creating a hidden input for each SelectedRole item you then disable the whole list.

Remove MVC 5 EditorTemplate additional ID

I am using this Editor Template for Dropdownlist with ViewBag/ViewData of same property Name
#model System.String
#*
For Using this Editor Template
- There should be a viewbag/viewdata (type SelectList) of same name as of calling Model's Property
*#
#{
var modelMetadata = ViewData.ModelMetadata;
// Get property name of the model
var propertyname = modelMetadata.PropertyName;
}
#if (ViewData[propertyname] == null)
{
#Html.DropDownList(propertyname , Enumerable.Empty<SelectListItem>(), "--Select--", new { #class = "form-control" })
}
else
{
#Html.DropDownList(propertyname , null, "--Select--", new { #class = "form-control" })
}
now using it as
#Html.EditorFor(i=>i.Country,"CustomDropDown")
I also have a ViewBag.Country as SelectList of countries.
Everything works fine, but now the Naming of the Control becomes
<select class="form-control" id="Country_Country" name="Country.Country">
how to remove the additional Country from the id and name?
Additional Info:
I could have just used the #Html.DropDownList("Country") but it doesn't allow me to add the css class to the control.
I think i found the Solution.
I changed the DropDownList to DropDownListFor with some changes
#if (ViewData[propertyname] == null)
{
#Html.DropDownListFor(m=>m, Enumerable.Empty<SelectListItem>(), "--Select--", new { #class = "form-control" })
}
else
{
#Html.DropDownListFor(m => m, ViewData[propertyname] as SelectList, "--Select--", new { #class = "form-control" })
}
this auto ViewData bind is kind of confusing at times. >_<

How do I add HtmlAttributes in mvc using ViewBag or Model values?

I have a textarea that I might want to disable in certain conditions. I want to send this information as a ViewBag parameter, but I can't figure out how to do it.
The textarea in my view looks like this
#Html.TextAreaFor(f => f.ProgressDetail, new { #class = "followUpProgress", ViewBag.DisableProgressDetail })
And in the controller I have something like this:
if(conditions)
ViewBag.DisableProgressDetail = "disabled=\"disabled\"";
The html output, however, is this:
<textarea DisableProgressDetail="disabled="disabled"" class="followUpProgress" cols="20" id="ProgressDetail" name="ProgressDetail" rows="2">
</textarea>
What you want is this:
#Html.TextAreaFor(f => f.ProgressDetail, new { #class = "followUpProgress", disabled = ViewBag.DisableProgressDetail })
Then in your controller, just make it:
ViewBage.DisableProgressDetail = "disabled";
The attribute if not specified comes from the name of the property, that is why you are getting an html attribute named for the ViewBag property. One way to get it working would be:
// in the view:
#Html.TextAreaFor(f => f.ProgressDetail, new { #class = "followUpProgress", ViewBag.disabled })
-------------------------------------------------------------
// in the controller
ViewBag.disabled = "disabled";
If you don't like that approach you might just set the disabled bit like this:
// in the view:
#Html.TextAreaFor(f => f.ProgressDetail, new { #class = "followUpProgress", disabled=ViewBag.DisableProgressDetail })
-------------------------------------------------------------
// in the controller:
if(conditions)
ViewBag.DisableProgressDetail = "disabled";
else
ViewBag.DisableProgressDetail = "false";
// or more simply
ViewBag.DisableProgressDetail = (conditions) ? "disabled" : "false";
It doesn´t work. You can try this:
//In the controller
if(Mycondition){ ViewBag.disabled = true;}
else { ViewBag.disabled = false;}
//In the view
#Html.TextBoxFor(model => model.MyProperty, ViewBag.disabled ? (object)new { #class = "MyClass", size = "20", maxlength = "20", disabled = "disabled" } : (object)new { #class = "MyClass", size = "20", maxlength = "20" })

Enabling & disabling a textbox in razor view (ASP.Net MVC 3)

I want to Enable or Disable a textbox based on the value (Model.CompanyNameEnabled).
The below code is not working. Please rectify.
#{
string displayMode = (Model.CompanyNameEnabled) ? "" : "disabled = disabled";
#Html.TextBox("CompanyName", "", new { displayMode })
}
#{
object displayMode = (Model.CompanyNameEnabled) ? null : new {disabled = "disabled" };
#Html.TextBox("CompanyName", "", displayMode)
}
You should pass htmlAttribute as anonymous object, with property names = html attribute names, property values = attribute values. Your mistake was that you were passing string instead of name=value pair
<input id="textbox1" type="text" #{#((Model.CompanyNameEnabled) ? null : new { disabled = "disabled" })}; />
Haven't tested it, but should work
A simple approach:
#Html.TextBoxFor(x => x.Phone, new { disabled = "disabled", #class = "form-control" })
As is already mentioned in this thread the suggested answer doesn't work in MVC5 anymore. There's actually an easy two step solution to that problem.
Assign a class to the HTML inputs you want to be disabled / enabled (id will do for a single item just as fine of course). In the example below I assigned a class 'switch-disabled' to the input.
#Html.TextBox("CompanyName", "", new { htmlAttributes = new { #class = "form-control switch-disable" } })
Use javascript(jquery) to enable / disable the disabled parameter in HTML. In my example below I do this at the page load.
<script>
$(document).ready(() => {
if(#Model.CompanyNameEnabled)
{
$('.switch-disable').attr("disabled", false);
}else{
$('.switch-disable').attr("disabled", true);
}
});
</script>

Resources