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