Can some one tell me why this bit of code
#Html.DropDownList("priority2", new SelectList(new[] { 1,2,3 }, Model.Priority))
gives me a fine dropdown to choose between 1,2,3
but this
#Html.DropDownList("priority", new SelectList(new [] {
new SelectListItem() { Value = "1", Text = "1. Priority" },
new SelectListItem() { Value = "2", Text = "2. Priority" },
new SelectListItem() { Value = "3", Text = "3. Priority" } },
Model.Priority))
gives me 3 options all saying 'System.Web.Mvc.SelectListItem'
What have I done wrong?
The SelectList() constructor uses reflection to generate IEnumerable<SelectListItem>. Where you do not specify the dataValueField and dataTextField properties, internally the method uses the .ToString() value of the object in the collection.
In the first example, you have a an array of values types so .ToString() outputs "1", "2" etc.
In the second example, you have an array of SelectListItem and its .ToString() method outputs "SelectListItem".
For the 2nd example to generate the correct html, it would need to be
#Html.DropDownList("priority", new SelectList(new []
{
new SelectListItem() { Value = "1", Text = "1. Priority" },
new SelectListItem() { Value = "2", Text = "2. Priority" },
new SelectListItem() { Value = "3", Text = "3. Priority" }
}, "Value", "Text", Model.Priority))
where the second parameter "Value" specifies the property name of SelectListItem to use for the value attribute of the option, and the 3rd parameter "Text" specifies the property to use for the options display text.
However, this is just pointless extra overhead (creating a 2nd SelectList from the original SelectList) and the last parameter Model.Priority is ignored when binding to a property.
Instead the 2nd example can be simply
#Html.DropDownListFor(m => m.Priority, new []
{
new SelectListItem() { Value = "1", Text = "1. Priority" },
new SelectListItem() { Value = "2", Text = "2. Priority" },
new SelectListItem() { Value = "3", Text = "3. Priority" }
})
Related
How can i show the value i mean the text instead of the ID value of static dropdownlist in razor view here is the code
<td>
#Html.DisplayFor(modelItem => item.Program_duration_Years)
</td>
and here it is in create view
#Html.DropDownListFor(model=>model.Program_duration_Years, new List<SelectListItem>
{
new SelectListItem{ Text="Select Duration", Value = "0" },
new SelectListItem{ Text="One year", Value = "1" },
new SelectListItem{ Text="Two Year", Value = "2" },
new SelectListItem{ Text="Three year", Value = "3" },
new SelectListItem{ Text="Four Year", Value = "4" },
new SelectListItem{ Text="Five year", Value = "5" },
}, new { #class = "form-control", #id = "ddlduration" })
#Html.ValidationMessageFor(model => model.program_type, "", new { #class = "text-danger" })
My drop down list is filled out like this at the moment and interacts with my model employeeModel:
#Html.DropDownListFor(x => employeeModel.JobTitle,
new SelectList(
new List<Object>
{
new { value = "Job Title 1", text = "Job Title 1"},
new { value = "Job Title 2", text = "Job Title 2"},
new { value = "Job Title 3", text = "Job Title 3"},
new { value = "Job Title 4", text = "Job Title 4"}
},
"value",
"text",
0), "Select...", new { #class = "form-control" })
However, I need a way to so that I can fill this out way more easily than having to either manually do this by hand. The easiest way I thought of doing this was through a CSV file. However, I have no clue as to how I can get my user to upload the file, then grab all the contents of it so that it can fill the above code snippet.
Has anyone ever done such a thing before?
You could add an Upload field where the user could select a text file containing the list of jobs. In this case I'm assuming that the file looks something like this:
Job Title 1
Job Title 2
Job Title 3
Job Title 4
Job Title 5
In your controller you'd have to get the property value and convert it from a relative Uri path to an absolute file path. Then use StreamReader to read through the file and add the items to a list, before using the values to create a list of SelectListItems and add them to your employeeModel.
var fileUri = CurrentPage.GetPropertyValue<string>("jobFile");
var filePath = System.Web.HttpContext.Current.Server.MapPath(fileUri);
var values = new List<string>();
using (var reader = new StreamReader(filePath))
{
values = reader.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.None).ToList();
}
employeeModel.Jobs = values.Select(x =>
{
return new SelectListItem
{
Value = x,
Text = x
};
});
Finally, render the drop down list in your view:
#Html.DropDownListFor(x => x.JobTitle, Model.Jobs, "Select...", new { #class = "form-control" })
I am getting BC30203 error in my ascx page.
BC30203: Identifier expected. (Line 4 - new[] )
Code:
<%= Html.DropDownList(
"",
new SelectList(
new[]
{
new { Value = "true", Text = "Yes" },
new { Value = "false", Text = "No" },
},
"Value",
"Text",
Model
)
) %>
What is missing ?
You are missing what to create:
new SelectList(
new ListItem[]
{
new ListItem { Value = "true", Text = "Yes" },
new ListItem { Value = "false", Text = "No" },
}
When using the new keyword, you must tell the compiler what you want to create it won't take a guess.
A red-flag that I see is that you are not giving a name to your SelectList.
<%= Html.DropDownList("MySelect",
new SelectList(
new[]
{
new SelectListItem() { Value = "true", Text = "Yes" },
new SelectListItem() { Value = "false", Text = "No" },
},
"Value",
"Text",
Model
)
) %>
The DropDownList method requires an IEnumerable<SelectListItem> as the 2nd parameter.
Try something like this
<%= Html.DropDownList(
"Name",
new List<SelectListItem>()
{
new SelectListItem() { Value = "true", Text = "Yes" },
new SelectListItem() { Value = "false", Text = "No" },
},
"Value",
Model
)
) %>
I have in my controller following code:
Values = new SelectList(new[] {10, 25, 50});
In my view I have:
<%= Html.DropDownList("selItemsPerPage1", Model.Items.Values,
new {
onchange="something", title="something"
});
In result output it displays list of
System.Web.Mvc.SelectList.
How do I make it display integers instead?
In the controller:
Values = new SelectList(
new[]
{
new SelectListItem { Value = "10", Text = "10" },
new SelectListItem { Value = "25", Text = "25" },
new SelectListItem { Value = "50", Text = "50" },
},
"Value", "Text"
);
or if you prefer:
var values = new[] { 10, 25, 50 }.Select(x => new SelectListItem
{
Value = x.ToString(),
Text = x.ToString()
});
Values = new SelectList(values, "Value", "Text");
and in the view:
<%= Html.DropDownList(
"selItemsPerPage1",
Model.Items.Values,
new {
onchange = "something",
title = "something"
}
) %>
I want to create this but with additional different values for the displayed options:
<%=Html.DropDownList("", new SelectList(new[] { "Alabama", "Alaska", "American Samoa", "Arizona", "Arkansas", "California", "Colorado", "Connecticut" }))%>
IE. Alabama would be shown but the value of this selection would be AL. And I do not want to use a ViewData.
<%=
Html.DropDownList("state", new [] {
new SelectListItem() {Text = "Alabama", Value = "AL"},
new SelectListItem() {Text = "Alaska", Value = "AK" }
}) %>
Something like this?:
<%= Html.DropDownList("state", new[] { "Alabama,AL", "Alaska,??", "American Samoa,??", "Arizona,??", "Arkansas,??", "California,??", "Colorado,??", "Connecticut,??" }
.Select(x => new SelectListItem {
Text = x.Split(',')[0],
Value = x.Split(',')[1],
Selected = x.Split(',')[0] == "Alabama"
})
) %>