I have the following DropDownListFor that displays a list of states for the user to choose from. How can I default the dropdown to be empty. It currently defaults to the first state in alphabetical order ("Alaska")
#Html.DropDownListFor(m => m.User.StateID,
new SelectList(Model.User.States.Where(filter => filter.Active && (filter.CountryID == 1))
.Select(item => new
{
ID = item.StateProvinceID,
Description = item.Name
}),
"ID",
"Description",
Model.User.StateID),
new { #data_bind = "value: user.StateID, enable:!isSiteUser()", tabindex = "10" })
I figured out that one of the posted questions did have the correct answer for my problem. I was just trying to do use it incorrectly. I was adding "string.Empty" to the wrong location. This is the solution.
#Html.DropDownListFor(m => m.User.StateID,
new SelectList(Model.User.States.Where(filter => filter.Active && (filter.CountryID == 1))
.Select(item => new
{
ID = item.StateProvinceID,
Description = item.Name
}),
"ID",
"Description",
Model.User.StateID),
string.Empty,
new { #data_bind = "value: user.StateID, enable:!isSiteUser()", tabindex = "10" })
Related
I have a problem for which I just can't find a solution.
Checking button like this works:
#Html.RadioButtonFor(x => x.an2[0], 3, Model.an2[0]==3
? new { id = "B", #checked = "checked" } : null)
But I also need to set id, how do I do that? This code:
#Html.RadioButtonFor(x => x.an2[0], 3, Model.an2[0]==3
? new { id = "B", #checked = "checked" } : new { id = "B" })
Gives error:
Type of conditional expression cannot be determined because there is no implicit conversion between 'AnonymousType#1' and 'AnonymousType#2'
Which is beyond my understanding.
I suppose there should be a trivial solution to this?
Cast them to object:
Html.RadioButtonFor(x => x.an2[0], 3, Model.an2[0]==3
? (object)new { id = "B", #checked = "checked" } : (object)new { id = "B" });
Check this link.
#{string id = ViewData.TemplateInfo.GetFullHtmlFieldId("radioTrue");}
#Html.RadioButtonFor(x => x.an2[0], 3, Model.an2[0]==3 ? new { id } : null)
A good example of this is here.
I am populating a dropdown like this
primaryDrop = new Dictionary<string, string>()
{
{"1", "Getting ready"},
{"2", "Starting"},
{"3", "All"},
{"4", "Other"}
};
PrimaryDrop = primaryDrop.Select(item => new SelectListItem
{
Value = item.Key,
Text = item.Value
});
And displaying it like this
#Html.DropDownListFor(m => m.SelectedPrimary, new SelectList(Model.PrimaryDrop, "Value", "Text"), "Select ...", new { #class = "form-control" })
How can I select a default value to 'Other' for example
Was thinking something like this
#Html.DropDownListFor(m => m.SelectedPrimary, new SelectList(Model.PrimaryDrop, "Value", "Text", new { SelectedItem = "Other" }), "Select ...", new { #class = "form-control" })
but doesn't work
You don't need to create an object to specify the selected item, you just need to give the value you want. In your case, Other has a value of 4:
#Html.DropDownListFor(m => m.SelectedPrimary,
new SelectList(ViewBag.PrimaryDrop, "Value", "Text", "4"),
"Select ...",
new { #class = "form-control" })
You can achieve it by server code, when you populate data
PrimaryDrop = primaryDrop.Select(item => new SelectListItem
{
Value = item.Key,
Text = item.Value,
Selected = item.Key == "4" // Or item.Value== "Others"
});
Or we can set value for SelectedPrimary of model
SelectedPrimary = "4"
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" : "")})
Benefit Set: </label><br />
<%: Html.DropDownListFor(model => model.bvODSMapping.Benefit_Set, Model.BenefitSet,new {id="BSet", style = "width:230px;" })%>
When I am using model=>model.bvODSMapping.Benefit_Set.Trim()
I am getting Value Can not be null.
can anybody help me out how to trim the string?
Thanks
I guess it would be the best to trim the values before passing the model to your view.
Nevertheless, this might help:
#Html.DropDownListFor(
model =>
model.bvODSMapping.Benefit_Set,
Model.BenefitSet.Select(
item =>
new SelectListItem
{
Selected = item.Selected,
Text = item.Text.Trim(),
Value = item.Value
}),
new { id = "BSet", style = "width:230px;" })
I have a EditorTemplate in a PartialPageView for a DateTime which has three DropDownLists for the day, month, and year. When I try to update the values the postback Date is always 01/01/0001. Any ideas?
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.DateTime?>" %>
<%= Html.DropDownListFor(x => x.Value.Day, Enumerable.Range(1, 31).Select(i => new SelectListItem { Value = i.ToString(), Text = i.ToString(), Selected = i == Model.Value.Day ? true: false})) %>
<%= Html.DropDownListFor(x => x.Value.Month, Enumerable.Range(1, 12).Select(i => new SelectListItem { Value = i.ToString(), Text = System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat.GetMonthName(i), Selected = i == Model.Value.Month ? true : false }))%>
<%= Html.DropDownListFor(x => x.Value.Year, Enumerable.Range(1900, 110).Select(i => new SelectListItem { Value = i.ToString(), Text = i.ToString(), Selected = i == Model.Value.Year ? true : false }))%>
To handle this case you might need to write a custom model binder.