assign a default placeholder value to a dropdown list menu - html

I am trying to assign a placeholder value to a drop down menu that holds state abbreviations from an entity model.
This is the example code I have tried, but that is not working, because
#Html.DropDownListFor(m => m.StateID, new SelectList(Model.States, "ID", "Abbreviations", Model.StateID), new { id = "StateID" + Model.ID, data_placeholder = "State" })
When I open the page, the first value that shows is AL for Alabama instead that the word "State"

You can use this overloaded version of DropDownListFor to specify default option :-
#Html.DropDownListFor(m => m.StateID, new SelectList(Model.States,"ID","Abbreviations"),
"State", new { id = "StateID" + Model.Id })
Also, Please note that there is no need to specify Model.StateID again in SelectList constructor because automatic selection of dropdown (selected value) will be taken care by first param i.e. m => m.StateID.

Related

DropDownListFor not populating selected value

Probably a frequently asked question, but I didn't found any answer matching my problem.
I got a SelectList like, which I want to show in a DropDown:
var selectList = new SelectList(listItems, "Text", "Value", selectedCustomer);
#Html.DropDownListFor(model => model.Name, selectList, "-- Select Customer --")
The list holds the correct values, one is selected. But the dropdownlist shows only the text "-- Select Customer --".
Populating listItems:
List<SelectListItem> listItems = new List<SelectListItem>();
foreach (Customer c in Model.GetAllCustomer())
{
listItems.Add(new SelectListItem { Text = c.Id, Value = c.Name });
}
To clarify my question: The dropdown works fine on most sites (it's in the layout page). But sometimes after a POST request it does not show any selected value. All the provided code is in the layout page.

When dropdownlitFor is disabled , value always passed as 0

This is my dropdownlistFor
#Html.DropDownListFor(m => m.objTicket.DepartmentId, new SelectList(Model.objTicket.Departments, "DepartmentId", "Department"),
"-- Select Department--", new { id = "Deptment"}, disabled="disabled")
How can I pass the value which is already stored in objTicket.DepartmentId ?
If i change remove disabled ="disabled", i get my correct value.
Form fields are submitted using Names instead of Ids. Disabled controls values wont be submitted by browsers. Place a #Html.HiddenFor field with same name and different Ids as given below.
#Html.DropDownListFor(m => m.objTicket.DepartmentId, new SelectList(Model.objTicket.Departments, "DepartmentId", "Department"),Department--", new { id = "Deptment"}, disabled="disabled")
#Html.HiddenFor(m => m.objTicket.DepartmentId, new { id="Deptment2" })

how to change a checkbox to 2 radio buttons. Value doesn't pass back 'true'

My table in the DB contains 2 fields. 'Title' and 'IsBusiness'(which is stored as a bool on wether the record is a business account or not.
When adding a new record on screen the editorFor is used to display a checkbox for 'IsBusiness' which passes back true or false.
#Html.EditorFor(x => x.IsBusiness)
#Html.ValidationMessageFor(x => x.IsBusiness)
I want to change this to 2 radio buttons. 'Product' and 'Business' which passes back false if product is selected and true is business is selected.
So far my code below keeps passing back false. It wont store 'True'...any ideas?
<label>#Html.RadioButtonFor(x => x.IsBusiness, "Business")Business</label>
<label>#Html.RadioButtonFor(x => x.IsBusiness, "Product")Product</label>
Try:
<label>#Html.RadioButtonFor(x => x.IsBusiness, true) Business</label>
<label>#Html.RadioButtonFor(x => x.IsBusiness, false) Product</label>
See also: ASP.NET MVC Yes/No Radio Buttons with Strongly Bound Model MVC

Get value of disabled drop down in asp.net mvc

I have an ASP.NET MVC application. I am having multiple drop-down list in my page (HTML SELECT), I have to disable them, as user goes on selected them one by one. When the user posts it back to the controller, I am getting null as the function (action method) paramters. I searched and found that HTML does not send value of disabled fields in the form data. Replacing disabled attribute with readonly would not work as it would render drop-down working.
I am generating the dropdowns dynamically using javascript as user goes on. So there isn't a single dropdown, but as many as user wants.
Can someone please tell me how should I get the values ?
One possibility is to make the dropdown list disabled="disabled" and include a hidden field with the same name and value which will allow to send this value to the server:
#Html.DropDownListFor(x => x.FooId, Model.Foos, new { disabled = "disabled" })
#Html.HiddenFor(x => x.FooId)
If you have to disabled the dropdown dynamically with javascript then simply assign the currently selected value of the dropdown to the hidden field just after disabling it.
This is the default behavior of disabled controls. I suggest you to add a hidden field and set the value of your DropDownList in this hidden field and work with this.
Something like:
//just to create a interface for the user
#Html.DropDownList("categoryDump", (SeectList)ViewBag.Categories, new { disabled = "disabled" });
// it will be send to the post action
#Html.HiddenFor(x => x.CategoryID)
You could also create your own DropDownListFor overload which accepts a bool disabled parameter and does the heavy lifting for you so your view isn't cluttered with if disablethisfield then ....
Something among these lines could do:
public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, bool disabled)
{
if (disabled)
return MvcHtmlString.Create(htmlHelper.HiddenFor(expression).ToString() + htmlHelper.DropDownListFor(expression, selectList, new { disabled="disabled" }).ToString());
else
return htmlHelper.DropDownListFor(expression, selectList);
}
There are 6 overloads for DropDownListFor alone so it's a lot of monkeycoding but it pays off in the end imho.
Create a hidden field with a specified Id and set it before disabling the drop-down-list.
In MVC,
#Html.DropDownListFor(x => x.FooId, Model.Foos)
#Html.HiddenFor(x => x.FooId, new { #id = "hdnFooId" })
In JQuery,
function handleDropDownListFooChange(){
// Get the selected value from drop-down-list before disabling it.
var selectedFooId = $('#FooId').val();
$('#FooId').prop("disabled", "disabled");
$("#hdnFooId").val(selectedFooId);
// Load any data the depends on selected FooId using `selectedFooId` variable.
}
The selected value will automatically be binded to Model.FooId.
before submit call $('#FooId').removeAttr('disabled')

How to set label value on Razor Html.DropDownList

i want to set the label value for a dropdownlist(not the default value, the label value) and i think im doing something wrong
#Html.DropDownList("cboCategoria", new SelectList(Model, "ID", "Nome"), new { #id = "cboCategoria", #label = "Categoria-pai: " })
You can do it a few ways, The label is seperate from the creation of the actual <select>:
<label>Categoria-pai: #Html.DropDownList(...)</label>
OR
<label for="cboCategoria">Categoria-pai:</label> #Html.DropDownList(...)
OR
#* This assumes you are creating the dropdown from a property named
cboCategoria in your Model *#
#Html.LabelFor(m => m.cboCategoria) #Html.DropDownList(...)
EDIT: I did want to note, that if you use the last method, you will want a [Display] attribute on your Model's property.