i have added drop with some static value but its showing error
#Html.DropDownListFor(model => model.MRStype, new SelectList(new List
{
new { Value = "0", Text = "--Select--" },enter code here
new {Value = "1", Text = "Internal Issue" },
new { Value = "2" , Text = "External Issue" }
},
"value",
"text"
), new { #class = "form-control", #id = "ddlSourceType" })
Change your Code to this one (use List instead of List)
#Html.DropDownListFor(model => model.GlobalStudentId, new SelectList(new List<object> { new { Value = "0", Text = "--Select--" }, new { Value = "1", Text = "Internal Issue" }, new { Value = "2", Text = "External Issue" } }, "value", "text"), new { #class = "form-control", #id = "ddlSourceType" })
In the view I have the following:
#Html.DropDownList("numberup", new List<SelectListItem>
{
new SelectListItem{Text = "1", Value = "1"},
new SelectListItem{Text = "2", Value = "2"}
}, "Select NumberUp", new { id = "numberup", #class = "form-control", #style = "width: auto; margin: 0 0 25px 0;" })
I would like to create the select list items dynamically instead of typing them out one by one. How do I do this?
Build a list of SelectListItems using Enumerable.Range and assign this to your DropDownList:
#{
var list = Enumerable.Range(1, 30).Select(x => x.ToString()).Select(x => new SelectListItem() { Text = x, Value = x }).ToList();
}
#Html.DropDownList("numberup", list, "Select NumberUp", new { id = "numberup", #class = "form-control", #style = "width: auto; margin: 0 0 25px 0;" })
HTH
I'm writing a form which uses drop down menus for value selection. I'd like to know if it is possible to post Value and Name of the SelectList back to the Controller or add values for every list item? I need to set LateralityId = Value and LateralityName = Text.
I'll just post some snippets of the code to get the idea across. The model I'm using is
public class Condition
{
public string LateralityName { get; set; }
public string LateralityId { get; set; }
}
and the values are posted with
#Html.DropDownListFor( m => m.Condition.LateralityId, GetLateralities() )
where
public static List<SelectListItem> GetLateralities()
{
return new List<SelectListItem>
{
new SelectListItem { Text = "", Value = "900" },
new SelectListItem { Text = "Left", Value = "1001" },
new SelectListItem { Text = "Right", Value = "1002" },
new SelectListItem { Text = "Midline", Value = "1003" },
new SelectListItem { Text = "Left Midline", Value = "1004" },
new SelectListItem { Text = "Right Midline",Value = "1005" },
new SelectListItem { Text = "Both Sides", Value = "1006" },
new SelectListItem { Text = "Unknown", Value = "990" },
new SelectListItem { Text = "Unspecified", Value = "997" },
new SelectListItem { Text = "N/A", Value = "999" },
};
}
Any help is much appreciated. Thanks!
Solution thanks to Stephen Muecke
In the controller I simply use
LateralityName = GetLateralities().FirstOrDefault( x => x.Value == m.Condition.LateralityId )?.Text;
I would like to have grouped items within an asp.net dropdownlist and I thought of using the SelectList functionality from MVC to my webforms project. Here's what I have tried and the result. Can you help please with the binding?
<asp:DropDownList runat="server" id="ddlGroupBySelGeo" AutoPostBack="True"
DataTextField="Text" DataValueField="Value"
SelectMethod="GetMapItems" OnSelectedIndexChanged="ddlGroupBySelGeo_OnSelectedIndexChanged">
</asp:DropDownList>
public SelectList GetMapItems(object sender, EventArgs e)
{
var items = new List<SelectListItem>();
var group1 = new SelectListGroup() { Name = "Select" };
var group2 = new SelectListGroup() { Name = "Group" };
items.Add(new SelectListItem() { Text = "display selected", Value = "select", Group = group1 });
items.Add(new SelectListItem() { Text = GetLocalResourceObject("Region").ToString(), Value = "Regional", Group = group2 });
items.Add(new SelectListItem() { Text = GetLocalResourceObject("Municipality").ToString(), Value = "Municipio", Group = group2 });
items.Add(new SelectListItem() { Text = GetLocalResourceObject("Locality").ToString(), Value = "Localidade", Group = group2 });
items.Add(new SelectListItem() { Text = GetLocalResourceObject("Neighborhood").ToString(), Value = "Bairro", Group = group2 });
return new SelectList(items);
}
Have you tried setting the Value and Text parameters in the SelectList before you return it?
return new SelectList(items, "Value", "Text");
I want to validate Date field of a birthday selection section. I have a dropdownlist with 1-31 days listed down and the default value is "0" and the text is "Day". Code is below:
public IEnumerable<SelectListItems> Date = new List<SelectListItems>
{
new SelectListItems{Value = "0", Text = "Day", Default = true},
new SelectListItems{Value = "1", Text = "1"},
new SelectListItems{Value = "2", Text = "2"},
new SelectListItems{Value = "3", Text = "3"},
new SelectListItems{Value = "4", Text = "4"},
new SelectListItems{Value = "5", Text = "5"},
new SelectListItems{Value = "6", Text = "6"},
new SelectListItems{Value = "7", Text = "7"},
new SelectListItems{Value = "8", Text = "8"},
new SelectListItems{Value = "9", Text = "9"},
new SelectListItems{Value = "10", Text = "10"},
new SelectListItems{Value = "11", Text = "11"},
new SelectListItems{Value = "12", Text = "12"},
new SelectListItems{Value = "13", Text = "13"},
new SelectListItems{Value = "14", Text = "14"},
new SelectListItems{Value = "15", Text = "15"},
new SelectListItems{Value = "16", Text = "16"},
new SelectListItems{Value = "17", Text = "17"},
new SelectListItems{Value = "18", Text = "18"},
new SelectListItems{Value = "19", Text = "19"},
new SelectListItems{Value = "20", Text = "20"},
new SelectListItems{Value = "21", Text = "21"},
new SelectListItems{Value = "22", Text = "22"},
new SelectListItems{Value = "23", Text = "23"},
new SelectListItems{Value = "24", Text = "24"},
new SelectListItems{Value = "25", Text = "25"},
new SelectListItems{Value = "26", Text = "26"},
new SelectListItems{Value = "27", Text = "27"},
new SelectListItems{Value = "28", Text = "28"},
new SelectListItems{Value = "29", Text = "29"},
new SelectListItems{Value = "30", Text = "30"},
new SelectListItems{Value = "31", Text = "31"}
};
My model is as below:
public class PaxInfoViewModel
{
public bool IsChild
{
get { return Age != null; }
}
[Required(ErrorMessage="Please select the born Month")]
public int BirthYear1 { get; set; }
[Range(2, 12, ErrorMessage="Please select the born Month")]
public int BirthMonth { get; set; }
[Required(ErrorMessage = "Please select the born Date")]
[Range(2, int.MaxValue, ErrorMessage = "Please select the born Date")]
public int BirthDate { get; set; }
}
My View is as below:
<div class="row">
<div class="editor-label control-label">#Html.Label("Child " + i + " Birthday ")</div>
<div class="span1">#Html.DropDownListFor(m => Model.Rooms[roomCount - 1].Children[i - 1].BirthDate, new SelectList(Model.DropDown.Date, "Value", "Text"), new { #class = "mini" })</div>
<div class="span2">#Html.DropDownListFor(m => Model.Rooms[roomCount - 1].Children[i - 1].BirthMonth, new SelectList(Model.DropDown.Month, "Value", "Text"), new { #class = "small" })</div>
<div class="errorformat">#Html.ValidationMessageFor(m => Model.Rooms[roomCount - 1].Children[i - 1].BirthMonth)
#Html.ValidationMessageFor(m => Model.Rooms[roomCount - 1].Children[i - 1].BirthDate)
</div></div>
This Date is need to validate. User must select a value other than the Default value. How can I do that.Please help.
Thank You.
Finally I figured it out. ASP.NET MVC 3 Unobtrusive validation only validate "input" elements.
Therefore I had to do some changes to the jquery.validate.unobtrusive.js code.
Here what i did:
On the jquery.validate.unobtrusive.js file, around code line 173(in my file) you can find this code line : $(selector).find(":input[data-val=true]").each(function (), change it to: $(selector).find("[data-val=true]").each(function ().
Also around line 289 you can find this code line : element = $(options.form).find(":input[name=" + fullOtherName + "]")[0], change it to : element = $(options.form).find("[name=" + fullOtherName + "]")[0]
And in my View file I add the validation attributes to the dropdownlist as below:
#Html.DropDownListFor(Yourmodelvariable, Yourlistitems, new Dictionary<string, object>() { { "data-val-range-min", "1" }, { "data-val-range-max", "31" }, { "data-val-range", "Please select the born Date" }, { "data-val", "true" }, { "class", "mini" } }) "
#Html.ValidationMessageFor(Yourmodelvariable)
You can add the validation attribute as you want to validate(In my case I want to validate a range), and it works for me as smooth as loose motion!.