On the edit screen I am allowing the user to edit a record.
But I dont want them to edit the lastUseDate. How can I set it to read only, instead of EditorFor LastUseDate, disabled?
#Html.EditorFor(x => x.Device.LastUseDate)
<%= #Html.TextBoxFor(x => x.Device.LastUseDate, new { #readonly = "readonly", disabled = "disabled", }) %>
Try this:
#Html.EditorFor(x => x.Device.LastUseDate, new { disabled = "disabled", #readonly = "readonly" })
It'll add disabled and readonly attribute to your textbox.
Related
My ViewModel has a property of selected and selectable. Both are boolean. I would like my view to have a checkbox that is enabled when selectable is true and disabled when selectable is false. What is the proper razor syntax to accomplish this ?
I tried the code below on a list of items in a table. Every row comes back with a disabled checkbox regardless of selectable value.
#Html.CheckBoxFor(modelItem => item.Selected, new { #disabled = !item.Selectable })
It is not easy to achieve this with an if condition inside the helper method because all the below markups will render a disabled chechbox.
<input type="checkbox" disabled>
<input type="checkbox" disabled="disabled">
<input type="checkbox" disabled="false">
<input type="checkbox" disabled="no">
<input type="checkbox" disabled="enabled">
This should work in the razor. Simple If condition and rendering what you want.
#if(item.Selected)
{
#Html.CheckBoxFor(modelItem => item.Selected)
}
else
{
#Html.CheckBoxFor(modelItem => item.Selected, new { #disabled = "disabled"})
}
You may consider writing a custom html helper which renders the proper markup for this.
This won't work because <input disabled="anything" /> will result in a disabled control. You need to only have a #disabled property when it should be disabled.
Try something like this:
#Html.CheckBoxFor(modelItem => item.Selected, item.Selectable ? (object)new {} : (object)new { #disabled = "disabled" })
Note that you might need to cast to (object)
The problem is when you have to add more than 1 HTML attribute.
That's a mess:
#if(item.Selected)
{
#Html.CheckBoxFor(modelItem => item.Selected, new { #data_foo = "bar"})
}
else
{
#Html.CheckBoxFor(modelItem => item.Selected, new { #data_foo = "bar", #disabled = "disabled"})
}
What I do to solve this is to use a IDictionary<string, object> that is previously loaded:
var htmlAttributes = new Dictionary<string, object>{
{"data-foo", "bar"}
};
if(!item.Selected)
{
htmlAttributes.Add("disabled", "disabled");
}
And then I create the checkbox component only once:
#Html.CheckBoxFor(modelItem => item.Selected, htmlAttributes)
Sorry, my previous answer was wrong.
The input-element will be disabled as soon as it gets the attribute disabled. It doesn't matter if the value is true, false. In HTML you can't set disabled to false.
So you will have to set the disabled attribute only when the condition is valid.
something like:
object attributes = null;
if (!item.Selectable)
{
attributes = new { disabled = "disabled"};
}
#Html.CheckBoxFor(modelItem => item.Selected, attributes)
I am using MVC 4, jQuery and Bootstrap plugin but I want to off the suggestion words, so I put autocomplte="off" but it is not working. Please suggestion me
My razor code is
#Html.TextBoxFor(m => m.Customer_Username, new { id = "logusrname",#autocomplete="off", #class = "form-control", #role = "form", #placeholder = "Username" })
Im beginner in the ASP.NET MVC. I am add drop down menu , but the CSS does not work correctly. How can I fix it? This is my code:
#Html.DropDownListFor(m => Model.Fields[i], new SelectList(Model.attrs[i].atributeDetails, "AtributeDetailId", "AtDetailVal", int.Parse(Model.Fields[i])), " ", new { Class = "form-control", disabled = "disabled",style = "width:575px;height:25px;font-size:small;" })
If you're saying that the styles associated with the 'form-control' class aren't working then it could be because the 'Class' word needs to be lowercase. I normally use #class="my-class". So maybe try the following:
#Html.DropDownListFor(m => Model.Fields[i], new SelectList(Model.attrs[i].atributeDetails, "AtributeDetailId", "AtDetailVal", int.Parse(Model.Fields[i])), " ", new { #class = "form-control", disabled = "disabled",style = "width:575px;height:25px;font-size:small;" })
I have three dropdownlistfor in a loop that do not show the correct value from the DB. They always default to the first entry. I have checked and double checked the DB and verified that it should be the second one in the list. The list is also created correctly. What am I missing?
#foreach (CustomerMeasurementProfile oProfile in Model.Customer.CustomerMeasurementProfiles.Where(m => m.DeletedDate == null))
{
<div class="valuesforoneprofile form-group form-group-tight col-md-2">
<div class="col-md-11" >
#Html.Hidden(string.Format("Customer.CustomerMeasurementProfiles[{0}].Id", i), oProfile.Id)
#Html.Hidden(string.Format("Customer.CustomerMeasurementProfiles[{0}].CustomerId", i), oProfile.CustomerId)
#Html.TextBox(string.Format("Customer.CustomerMeasurementProfiles[{0}].Name", i), oProfile.Name, new { #class = "form-control input-sm" })
</div>
<div class="col-md-11" style="text-align:center">
#Html.CheckBox(string.Format("DeleteProfiles[{0}]", i), Model.DeleteProfiles[i])
</div>
<div class="col-md-11" style="padding-top:4px;">
#Html.DropDownListFor(m => oProfile.BodyTypeShoulderId, new SelectList(Model.BodyTypeShoulders, "Id", "Name"), new { #class = "form-control input-sm-select" })
</div>
<div class="col-md-11" style="padding-top:4px;">
#Html.DropDownListFor(m => oProfile.BodyTypePostureId, new SelectList(Model.BodyTypePosture, "Id", "Name"), new { #class = "form-control input-sm-select" })
</div>
<div class="col-md-11" style="padding-top:4px;">
#Html.DropDownListFor(m => oProfile.BodyTypeChestId, new SelectList(Model.BodyTypeChest, "Id", "Name"), new { #class = "form-control input-sm-select" })
</div>
If you want to set the selected value that is coming in Model. You need to do it like this:
#Html.DropDownListFor(m => oProfile.BodyTypeShoulderId,
new SelectList(Model.BodyTypeShoulders,
"Id",
"Name",
oProfile.BodyTypeShoulderId),
new { #class = "form-control input-sm-select" })
The above code will set the dropdown selected value to whatever is in the current Model object BodyTypeShoulderId
The first argument of DropDownListFor tells that on form post drop down selected value will be mapped with the Model property which is set there (we are passing m => oProfile.BodyTypeShoulderId) but this not sets selected Value.
For setting selected value you have to pass SelectList fourth parameter using this overload of SelectList class which is object selectedValue
Unfortunately #Html.DropDownListFor() behaves a little differently than other helpers when rendering controls in a loop. For a single object
#Html.DropDownListFor(m => oProfile.BodyTypeShoulderId, new SelectList(Model.BodyTypeShoulders, "Id", "Name"))
would work fine (if the value of BodyTypeShoulderId matches the value of one of the options, then that option would be selected). Ehsan has shown a work around when using it in a loop, however you have a few other issues in you code, not the least is that many of your properties will not post back correctly to a collection because your using a foreach loop rather than a for loop (which is generating duplicate name and id attributes). Your also generating a new SelectList in each iteration of the loop which is not very efficient.
You can solve both these and the dropdown selection issue by using an EditorTemplate. Assuming property CustomerMeasurementProfiles is typeof CustomerMeasurementProfiles, then
CustomerMeasurementProfiles.cshtml (add this to Views/Shared/EditorTemplates or Views/YourController/EditorTemplates)
#model CustomerMeasurementProfiles
#Html.HiddenFor(m => m.ID)
#Html.HiddenFor(m => m.CustomerId)
....
#Html.DropDownListFor(m => m.BodyTypeShoulderId, (SelectList)ViewData["shoulders"])
....
In the view model, add properties for the SelectList's and filtered collection of the items to display
IEnumerable<CustomerMeasurementProfiles> ActiveCustomerMeasurementProfiles { get; set; }
public SelectList BodyTypeShoulderList { get; set; }
....
and assign those values in the controller
Then in the main view
#using (Html.BeginForm())
{
...
#Html.EditorFor(m => m.ActiveCustomerMeasurementProfiles, new { shoulders = Model.BodyTypeShoulderList })
...
Using #Html.EditorFor() with a collection will correctly name the controls (and correctly select the right options) and on post back, ActiveCustomerMeasurementProfiles will now be correctly populated with all its properties.
I'm going to bind data to a label using MVC4,But it's not success..Data come to model.
Here is my code.
<div class="newclass">
#Html.LabelFor(m => m.NewsTeam.NewsMgr, new { style = "width:100%", #class = "newcssval" })
</div>
Try like this:
#Html.LabelFor(m => Model.NewsTeam.NewsMgr)
or use DisplayFor
#Html.DisplayFor(m => m.NewsTeam.NewsMgr)
Hope it helps
This will only give you the name of the field (or it's display value from the class). I suspect what you need is the following.
#Html.TextBoxFor(m => m.NewsTeam.NewsMgr,
new { #class = "newcssval", #readonly = true })
This will give you a read-only text box wuth the content of your model data.
Hope that helps.
If your intent is to simply show the value of the property, use the Display or DisplayFor helper:
#Html.DisplayFor(m => m.NewsTeam.NewsMgr)
Or, forget the helper entirely:
#Model.NewsTeam.NewsMgr
If the property contains HTML, you'll need to use the Raw helper:
#Html.Raw(Model.NewsTeam.NewsMgr)
Use this,
#Model.NewsTeam.NewsMgr
It will just show the value of property as label
Or Use,
#Html.DisplayFor(m => m.NewsTeam.NewsMgr)