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" })
Related
I have an editor field in asp.net which needs to be integer input only between 1 and the size of a list. Atm i got this as code
#Html.Editor("Prioriteit" + item, new { htmlAttributes = new { #type = "number", min = "0",step = "1", value = "0" } })
this isn't to the maximum of the list, I know this. However when i open the page i can still add text.
View with text in editor
Someone can help me out?
thanks in advance
Changed the code for the sake of someone linking it to another question
code
#Html.TextBox("Prioriteit" + item,null, new { htmlAttributes = new { #type = "number", min = "0",step = "1", value = "0" } })
still the same
`
You can also do the following:
<input type="number" min="0" step="1" value="0" name="Prioriteit#item" />
Try this:
#Html.TextBox("Prioriteit" + item, "0", new { #type="number", min = "0", step = "1" })
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" }
})
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
)
) %>
Does anyone know how to pass html attributes like "data-date" to the Html.TextBox()??
It is used from the bootstrap datepicker.
Is there a way around it?
#Html.TextBoxFor(p => p.DateFrom, new { #class = "small", type = "date", "data-date"="12-02-2012" })
You have to use underscore instead of '-'.
So what you're after would go something like this:
#Html.TextBoxFor(p => p.DateFrom, new { #class = "small", #type = "date", #data_date="12-02-2012" })
Use:
new Dictionary<string, object> { { "class", "small" }, {"type", "date"} { "data-date", "12-02-2012" } }
instead of:
new { #class = "small", type = "date", "data-date"="12-02-2012" }
I was trying to add the date picker to the Html helper using bootstrap. rudeovski ze bear's answer made it easier.
This is how it worked for me.
#Html.TextBox("startdate", null
, new {#class = "type", #type = "date", #data_date="12-02-2012" })
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"
}
) %>