Dynamically fill #Html.DropDownList with numbers from 1 - x - asp.net

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

Related

Error "An item with the same key has already been added" in asp.net MVC

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

How to bind highcharts from database using DotNet.Highcharts in asp.net

how to get this data "{ 29.9, 71.5, 106.4}" from database???
DotNet.Highcharts.Highcharts chart = new DotNet.Highcharts.Highcharts("chart")
.InitChart(new Chart { Type = ChartTypes.Bar })
.SetTitle(new Title { Text = "Wind speed during two days" })
.SetLegend(new Legend { Enabled = false })
.SetXAxis(new XAxis{
Categories = new[] { "Jan", "Feb", "Mar" }
}).SetSeries(new Series{
Data = new Data(new object[] { 29.9, 71.5, 106.4})
});
ltrChart.Text = chart.ToHtmlString();
I use this code and its work.....
DataTable dt2 = new DataTable();
dt2 = Chart_BL.fetch_Chart_Data(1);
decimal[] ar1 = new decimal[dt2.Rows.Count];
for (int i = 0; i < dt2.Rows.Count; i++)
{
ar1[i] = Convert.ToDecimal(dt2.Rows[i]["age"].ToString());
}
DotNet.Highcharts.Highcharts chart2 = new DotNet.Highcharts.Highcharts("chart_2")
.InitChart(new Chart { Type = ChartTypes.Bar })
.SetTitle(new Title { Text = "" })
.SetLegend(new Legend { Enabled = true })
.SetXAxis(new XAxis
{
Categories = new String[] { "10th", "11th", "10th", "11th" }
})
.SetSeries(new Series
{
Data = new Data(ar1.Select(x => (object)x).ToArray())
});
enter image description here

MVC drop down list input post value and name

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;

Using an MVC SelectList in webforms

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

How to remove the link if value is 0 using asp.net mvc

awesome worked fine for me.. due to security I am editing my code..
thanks
Update:
Replace
<%=Html.ActionLink(e.ExceptionTypeName, "VirtualScrollingDataRequested", Model.exceptionCategory.GetControllerName(), new { C_EXCPT_TYPE = e.ExceptionTypeID, GUI_SPEC_STAT_DSPL = 2, C_EXCPT_CATG = Model.exceptionCategory.Id, #ASSET_CLASS = string.Empty, #INST_MNEM = string.Empty, #_lock = "ALL" }, new { #title = e.BuildGridTitle(2, e.ExceptionTypeName) })%>
with
<%=e.workedexceptions == 0 ? e.ExceptionTypeName : Html.ActionLink(e.ExceptionTypeName, "VirtualScrollingDataRequested", Model.exceptionCategory.GetControllerName(), new { C_EXCPT_TYPE = e.ExceptionTypeID, GUI_SPEC_STAT_DSPL = 2, C_EXCPT_CATG = Model.exceptionCategory.Id, #ASSET_CLASS = string.Empty, #INST_MNEM = string.Empty, #_lock = "ALL" }, new { #title = e.BuildGridTitle(2, e.ExceptionTypeName) }) %>

Resources