I am trying to bind the drop down in MVC In model but i am getting error My code is as follows:
private IEnumerable<System.Web.Mvc.SelectListItem> _property;
public IEnumerable<System.Web.Mvc.SelectListItem> TestPapers
{
get
{
Controller _controller = new Controller();
_property = _controller.BindTestPaperDropdown(string.Empty);
return _property;
}
set
{
_property = value;
}
Can any body say where i am wrong?
This could also work :
<select name="sdfsdf">
#foreach selectOption in ViewBag['SelectOptions']
<option value="#selectOption.Value">#selectOption.Text</option>
#endforeach
</select>
Just gotta put everything in the viewbag at the controller
(The example is in Razor)
With aspx it would be something like this i think ... but it's been a while since i wrote any aspx so i might miss something but that's the main idea
<select name="sdfsdf">
<% foreach(var p in products) { %>
<option value="<% p.Value %>"> <% p.Text %> </option>
<% } %>
</select>
I am sharing a link where you can explore various ways to bind dropdown list in mvc.
http://www.c-sharpcorner.com/UploadFile/deveshomar/ways-to-bind-dropdown-list-in-Asp-Net-mvc/
Related
When I post the form to the Controller, the binding on the VehiclePlate in asp-for gets the value of the select option (#vehicle.Category) but I want to get the text of the option (#vehicle.Plate) in the Controller. How can I do that?
This is my select:
#model Exam
...
<select asp-for="VehiclePlate" id="listVehicle" data-init-plugin="select2" style="width: 100%">
#foreach (var vehicle in ViewBag.Vehicles)
{
<option value="#vehicle.Category">#vehicle.Plate</option>
}
</select>
And this is my Controller:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create(Exam exam)
{
...
}
The select tag has an asp-items attribute which you can set to a SelectList, e.g
<select asp-for="VehiclePlate" asp-items='new SelectList(ViewBag.Vehicles, "Plate", "Plate")'>
</select>
The "Plate", "Plate" part means, that the select uses Plate as value and as text for the dropdown.
I'm still getting used to changing over from asp.net to asp.net mvc and I know that it doesn't use on action commands but I'm trying to change the text of a label based on when a user selects an item from a dropdownlist. I'm really not sure where to start :(
You can easily get this done using some jQuery, here I made a Fiddle for you.
Below is how you HTML should look like, incase you are using pure HTML in your views or even if you are using #Html.LabelFor or #Html.DropDownListFor
HTML
<label id="myLabel">Select a fruit:</label>
<select id="fruitSelector">
<option val="">None</option>
<option val="apple">apple</option>
<option val="orange">orange</option>
<option val="mango">mango</option>
</select>
jQuery
$("#fruitSelector").change(function(){
$("#myLabel").text("Fruit has been selected");
});
Related help
https://stackoverflow.com/a/16828702/1182982
https://stackoverflow.com/a/14606324/1182982
Here is the simple example using jquery
#Html.DropDownList("State", ViewBag.StateName as SelectList,
"Select a State",
new { id = "State" })
<label id="lbl1"></label>
<script type="text/jscript">
$(function () {
$('#State').change(function () {
$('#lbl1').text($('#State').val());
});
});
</script>
You can try something like this
<label id="item">Selected Item: </label>
<select id="selector">
<option value="">None</option>
<option value="JS">JavaScript</option>
<option value="aspnet">Asp.net</option>
<option value="mvc">Asp.Net MVC</option>
</select>
<label id="result"></label>
$("#selector").change(function()
{
$("#result").text($(this).val());
});
http://jsfiddle.net/PLbnS/
To start with, your aspx should look like this:
<asp:DropDownList AutoPostBack="true" runat="server" ID="myListDropDown"
CssClass="text" OnSelectedIndexChanged="myListDropDown_Change" />
And your code-behind file:
private void myListDropDown_SelectedIndexChanged(object sender, System.EventArgs e)
{
//put your code here
}
Simple :
Write this code in your SelectedValueChanged Event of dropdownlist :-
private void dropdownlist_SelectedValueChanged(object sender, EventArgs e)
{
dropdownlist.Items["abc"]=label.text;
dropdownlist.Items["xyz"]=label.text;
}
I want to be able to set the first value in my dropdown list as "Select a poll", and then load all my select items from my viewdata respectively. How would I go about doing that? My block of code right now looks something like this:
<select name="ddlPolls" id="ddlPolls">
<% foreach (var item in (string[][])ViewData["returnPolls"])
{ %> <option value="<%= item[0] %>"><%= item[1] %></option><% } %>
</select>
This returns a dropdown list of my polls, but I want the first value in that dropdown list to be "Select a poll" so that I can use jquery to perform .change ajax functions to load another dropdown list. Any help is much appreciated!! :D
Just write your code as follow:
<select name="ddlPolls" id="ddlPolls">
<option value="0">Select a poll</option>
<% foreach (var item in (string[][])ViewData["returnPolls"])
{ %> <option value="<%= item[0] %>"><%= item[1] %></option><% } %>
</select>
This question already has answers here:
What HTML helper do I use to create a simple dropdownlist that doesn't take in any variables?
(2 answers)
Closed 2 years ago.
I'm trying to do something simple - I'm starting to learn MVC and I'm getting a tad confused. I have a dropdown that produces the following
<selectid="selectMenu" name="selectMenu">
<option value="Weekly">Weekly</option>
<option value="Monthly">Monthly</option>
<option value="Quarterly">Quarterly</option>
<option value="Annually">Annually</option>
</select>
thus I added the following to my view -
<% string[] rate = new string[]{"Weekly","Monthly","Quarterly","Annually"}; %>
<%: Html.DropDownList("selectMenu", new SelectList(rate))%>
However this produced the following:
<select DataTextField="" DataValueField="" Items="System.String[]" SelectedValue="" SelectedValues="" id="selectMenu" name="selectMenu">
<option>Weekly</option>
<option>Monthly</option>
<option>Quarterly</option>
<option>Annually</option>
</select>
How do I get the value of each option to be the same as the text?
Perhaps I should stick to JavaScript?
You could just use a loop and not use a html helper (syntax might be off)
<select id="selectMenu" name="selectMenu">
<% string[] rates = new string[]{"Weekly","Monthly","Quarterly","Annually"}; %>
<% foreach (string rate in rates) { %>
<option value="<%: rate %>"><%: rate %></option>
<% } %>
</select>
If your just starting out, I'd recommend using the Razor View engine in asp.net-mvc3. The syntax would be:
<select id="selectMenu" name="selectMenu">
#( string[] rates = new string[]{"Weekly","Monthly","Quarterly","Annually"}; )
#foreach (string rate in rates)
{
<option value="#rate">#rate</option>
}
</select>
<% var rate = new Dictionary<string, string> { { "Weekly", "Weekly" }, { "Monthly", "Monthly" }, { "Quarterly", "Quarterly" }, { "Annually", "Annually" } }; } %>
<%: Html.DropDownList("selectMenu", new SelectList(rate, "Key", "Value"), "--please select--") %>
I would like to pass a date from a drop down list to my ID in the Html.BeginForm. This is what I have so far.
<%using (Html.BeginForm("Inquiry", "Billing", new { Date = ????? }, FormMethod.Post))
<select id="myList" name="myList" >
<option>Term - Balance</option>
foreach (var item in Model.Term)
{ %>
<option value="/<%=String.Format("{0:yyyyMMdd}", item.Date) %>" >
<%=item.Date.ToShortDateString()%> - <%=String.Format("{0:C}", (item.Balance))%></option>
<% } %>
</select>
Any suggestions?
You can add additional attributes to the BeginForm method like this:
<% using (Html.BeginForm("Inquiry", "Billing",
FormMethod.Post, new { id = myDate.ToString() }))
{ %>
...
<%} %>
If you're asking how to make the form ID change when a user changes the selection on a dropdownlist on the form, then you'll need to use javascript for that. The code above only runs once at render time, so if your myDate variable isn't set by then, then you can't get it on the server side.