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"
Related
I'm kinda new to ASP.NET so i'm having some trouble to find out how I can do this. I have this problem where the user will give an amount(let's say 5) and then I have to create 5 textbox for the user to type some number in. My logic is, the user type the number amount and I create a html.editorfor foreach number.the problem is that I need the value from each editor.
like this.
<div>
#for (int i = 0; i < Model.qtde; i++)
{
#Html.EditorFor(c => c.numeros, new
{
htmlAttributes =
new
{
#class = "form-control input-lg",
autofocus = true,
#type = "number",
min = 0,
max = 1000
}
})
#Html.ValidationMessageFor(model => model.numeros, "",
new { #class = "text-danger" })
}
</div>
qtde: number amount that the user typed.
numeros: the number that the user typed.
If more information is need I'll update accordingly.
Use following code which will give you correct values (numeros[i] will give you value of each editor) and declare numeros as List or array:
#for (int i = 0; i < Model.qtde; i++)
{
#Html.EditorFor(c => c.numeros[i], new
{
htmlAttributes =
new
{
#class = "form-control input-lg",
autofocus = true,
#type = "number",
min = 0,
max = 1000
}
})
#Html.ValidationMessageFor(model => model.numeros[i], "",
new { #class = "text-danger" })
}
Say if I have this textbox
#Html.TextBoxFor(x => x.Name, new {id = "add-name", data_parsley_maxlength = "1" })
How do I write my LabelFor so that it references "add-name"?
Or do I have to use #Html.Label?
I have two Dropdownlist as follows
#Html.DropDownList("ddl_SearchBy", new List<SelectListItem>
{
new SelectListItem{Text="PA Number",Value="PA"},
new SelectListItem{Text="Customer Code" ,Value="CustCode"},
new SelectListItem{Text="Customer Name" ,Value="CustName"},
new SelectListItem{Text="Order Type" ,Value="OrderType"},
new SelectListItem{Text="Discount Type" ,Value="DiscType"},
new SelectListItem{Text="EIC", Value="EIC"},
new SelectListItem{Text="Status", Value="status"}
}, "-Select Criteria-", new { #class = "form-control", #id = "ddl_SearchBy" }
#Html.DropDownList("ddl_OrderType", Model.lstOrderType, "-Select Order Type-", new { #class = "form-control", #id = "ddlOrderType"})
#Html.DropDownList("ddl_Status", Model.lstStatus, "-Select Status-", new { #class = "form-control", #style = "display:none", #id = "ddl_Status" })
In Jquery I have done something like this
$('#ddl_SearchBy').change(function () {
if ($('#ddl_SearchBy').val() == 'status') {
$("#ddlDiscountType").hide();
$("#ddlStatus").show();
}
});
but when i changed event ddlstatus not shown
You can change visibility option by putting them in the DIV and make DIV hide and show depends on your requirement.
Please try this.
You can do following two changes,
1). In CSHTML File
#Html.DropDownList("ddl_Status", Model.lstStatus, "-Select Status-", new { #class = "form-control", #style = "display:none", #id = "ddl_Status" })
change id to "ddlStatus", like follows,
#Html.DropDownList("ddl_Status", Model.lstStatus, "-Select Status-", new { #class = "form-control", #style = "display:none", #id = "ddlStatus" })
2). in JS File
$('#ddl_SearchBy').change(function () {
if ($('#ddl_SearchBy').val() == 'status') {
$("#ddlDiscountType").hide();
$("#ddl_Status").show();
}
});
If you wrap your function with $(document).ready(function(){ });
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" : "")})
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" })