Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post May 04, 2012 06:06 PM by mm10
Member
218 Points
103 Posts
May 03, 2012 11:26 AM|LINK
i have created a simple drop down like this:
<select name="employees"> @foreach (var obj in Model.Employees) { <option value="@obj.EmployeeID">@obj.EmployeeFirstName</option> } </select>
now i want to ask that how can i recieve the selected value in the controller?
228 Points
52 Posts
May 03, 2012 11:47 AM|LINK
Hey
I think you are using classic asp.The code to create and read the value of dropdown control is as follows
<%
String drop_downvalue=request.form("employees")
Response.write(drop_downvalue.tostring())
%>
or the value can be used using javascript as follows
<script language="javascript">
function dropdownvalue()
{
var employees=document.formid.employees.value;
if (employees==0) { alert("\n Please select the Value of Employee from list."); document.formid.employees.focus(); return false; } return true; }
</script>
and call the function on click event of the button
Hope it will help you.
All-Star
134832 Points
21599 Posts
Moderator
MVP
May 03, 2012 12:01 PM|LINK
asad.malik <select name="employees">
asad.malik i want to ask that how can i recieve the selected value in the controller?
add to the action a parameter named employees
May 04, 2012 04:59 PM|LINK
can u explain a bit more, its not working for me..
Thanks
Regards
Asad
May 04, 2012 06:03 PM|LINK
asad.malik can u explain a bit more, its not working for me..
1. What is not working?
2. Please follow tutorials from http://www.asp.net/mvc
Contributor
6395 Points
1182 Posts
May 04, 2012 06:06 PM|LINK
View: @Html.DropDownListFor(m => m.SelectedEmployeeId, new SelectList(Model.Employees, "EmployeeID", "EmployeeFirstName", Model.SelectedEmployeeId), "-- Select Employee --")
Model: public class Model { public int SelectedEmployeeId {get;set;} ..... }
asad.malik
Member
218 Points
103 Posts
How to get the value of dropdown list in controller?
May 03, 2012 11:26 AM|LINK
i have created a simple drop down like this:
<select name="employees">
@foreach (var obj in Model.Employees)
{
<option value="@obj.EmployeeID">@obj.EmployeeFirstName</option>
}
</select>
now i want to ask that how can i recieve the selected value in the controller?
Regards
Asad Malik
Mark as answer if it helps
poojajoon
Member
228 Points
52 Posts
Re: How to get the value of dropdown list in controller?
May 03, 2012 11:47 AM|LINK
Hey
I think you are using classic asp.The code to create and read the value of dropdown control is as follows
<select name="employees">
@foreach (var obj in Model.Employees)
{
<option value="@obj.EmployeeID">@obj.EmployeeFirstName</option>
}
</select>
<%
String drop_downvalue=request.form("employees")
Response.write(drop_downvalue.tostring())
%>
or the value can be used using javascript as follows
<script language="javascript">
function dropdownvalue()
{
var employees=document.formid.employees.value;
if (employees==0)
{
alert("\n Please select the Value of Employee from list.");
document.formid.employees.focus();
return false;
}
return true;
}
</script>
and call the function on click event of the button
Hope it will help you.
ignatandrei
All-Star
134832 Points
21599 Posts
Moderator
MVP
Re: How to get the value of dropdown list in controller?
May 03, 2012 12:01 PM|LINK
add to the action a parameter named employees
asad.malik
Member
218 Points
103 Posts
Re: How to get the value of dropdown list in controller?
May 04, 2012 04:59 PM|LINK
ignatandrei
can u explain a bit more, its not working for me..
Thanks
Regards
Asad
Regards
Asad Malik
Mark as answer if it helps
ignatandrei
All-Star
134832 Points
21599 Posts
Moderator
MVP
Re: How to get the value of dropdown list in controller?
May 04, 2012 06:03 PM|LINK
1. What is not working?
2. Please follow tutorials from http://www.asp.net/mvc
mm10
Contributor
6395 Points
1182 Posts
Re: How to get the value of dropdown list in controller?
May 04, 2012 06:06 PM|LINK
View:
@Html.DropDownListFor(m => m.SelectedEmployeeId, new SelectList(Model.Employees, "EmployeeID", "EmployeeFirstName", Model.SelectedEmployeeId), "-- Select Employee --")
Model:
public class Model
{
public int SelectedEmployeeId {get;set;}
.....
}