Html.DropDownList binding integers - asp.net-mvc

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"
}
) %>

Related

Static dropdown list showing ID value instead of text on index view

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" })

dotnethighcharts - pointwidth

Im using dotnethighcharts and want the columns to have a pointwidth=20, but I cant get it to work with dotnethighcharts, any suggestions how to make that?
Highcharts chart;
chart = new Highcharts("chart")
.InitChart(new Chart { DefaultSeriesType = ChartTypes.Column })
.SetXAxis(new XAxis
{
Categories = new[] { "1", "2" },
Title = new XAxisTitle { Text = string.Empty }
})
.SetPlotOptions(new PlotOptions
{
Bar = new PlotOptionsBar
{
DataLabels = new PlotOptionsBarDataLabels { Enabled = true }
},
})
.SetSeries(new[]
{
new Series {Name = "1", Data = new Data(new object[] {1})},
new Series {Name = "2", Data = new Data(new object[] {4})}
});
Here is an example how to us pointwidth
http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/plotoptions/column-pointwidth-20/
The answer - I was missing this
Column = new PlotOptionsColumn
{
PointPadding = 0.2,
PointWidth = 40,
BorderWidth = 0
}
Here is all code
Highcharts chart;
chart = new Highcharts("chart")
.InitChart(new Chart { DefaultSeriesType = ChartTypes.Column })
.SetXAxis(new XAxis
{
Categories = new[] { "1", "2" },
Title = new XAxisTitle { Text = string.Empty }
})
.SetPlotOptions(new PlotOptions
{
Column = new PlotOptionsColumn
{
PointPadding = 0.2,
PointWidth = 40,
BorderWidth = 0
},
Bar = new PlotOptionsBar
{
DataLabels = new PlotOptionsBarDataLabels { Enabled = true }
},
})
.SetSeries(new[]
{
new Series {Name = "1", Data = new Data(new object[] {1})},
new Series {Name = "2", Data = new Data(new object[] {4})}
});

submit form at dropdown change event in mvc4

here is my code
<div id="pro_pag2">
#using (Html.BeginForm("Product", "Main", new { customerId = mdlLoginData.CustomerID, GroupID = ViewBag.GroupID, page = 1 }, FormMethod.Post, new { }))
{
#Html.DropDownList("PageSize", new SelectList(new Dictionary<string, string> {{ "18", "18" }, { "12", "12" },{ "6", "6" } }, "Key", "Value"), new { #class = "pro_pag_tf1" })
}
</div>
my question is that how do I submit form at selection change of the dropdown
You can use jQuery. Give the drop down list an id, like so.
<div id="pro_pag2">
#using (Html.BeginForm("Product", "Main", new { customerId = mdlLoginData.CustomerID, GroupID = ViewBag.GroupID, page = 1 }, FormMethod.Post, new { }))
{
#Html.DropDownList("PageSize",
new SelectList(new Dictionary<string, string> {...}, "Key", "Value"),
new { #class = "pro_pag_tf1", id = "pagesizelist" })
}
</div>
Then use jQuery to submit its parent form
$('#pagesizelist').on('change', function(event){
var form = $(event.target).parents('form');
form.submit();
});

Identifier expected error in ascx page in MVC

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
)
) %>

Html.DropDownList with Option and Value set NOT using ViewData

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"
})
) %>

Resources