Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi to all,
Here I'm doing dropdown list in MVC. But now i want textbox/dropdown.Here i attached my code Kindly help me.Also when you start typing , the drop down should start filtering itself..For example type Ti will show only the state starting with Ti. Hope you understand what I mean.

DESCRIPTION:-
Customer is my module. in that module there r so many fields like name,country,state,city.for country state city i want textbox/dropdown. here i attached my code.
in model:-
C#
public DbSet<State> State { get; set; }

in view:-
C#
@Html.DropDownList("State_Name", (IEnumerable<SelectListItem>)ViewData["ViewState"], new { id = "State_Name", @class = "form-control control-text" })

in controller:-
C#
List<SelectListItem> list = new List<SelectListItem>(); 
list.Add(new SelectListItem { Text = "Select Country", Value = "Select Country" });
//set default text for DropdownList 
var cat = (from c in db.Country select c).ToArray();
//get data from database using linq 
for (int i = 0; i < cat.Length; i++) {
    list.Add(new SelectListItem { 
        Text = cat[i].Country_Name, Value = cat[i].Country_Cde.ToString() }); } ViewData["ViewCountry"] = list; 

above code i'm using viewdata.i want using model
Posted
Updated 24-May-19 1:26am
v3

Use jQuery to do the same.
JavaScript
$(function () {
       $("[name='DropdownID']").change(function () {
           $("#TextBoxID").val($(this).val());
       });
   });


--Amy
 
Share this answer
 
Comments
JOTHI KUMAR Member 10918227 29-Jul-14 3:27am    
sorry i cant understand this explain me briefly
_Amy 29-Jul-14 4:17am    
You can call change event of dropdownlist using jQuery and set the selected value of dropdown to textbox.
Make your dropdown like this: This is a sample code. Change it as your requirement. You can use JS like below to fill text box.

If you find help from this , please marked as answered.

C#
@Html.DropDownList("jobID", null, new {@id="job"})

and taskname text box :
C#
@Html.EditorFor(model => model.taskName, new {@id="taskname"})

Do like this, write jquery event for dropdown index change event, and send ajax call to get Task Name against the job Id selected from the dropdown list:

JavaScript
$('select#job').change(function(){
var JobId =$(this).val();
// Send Ajax call and get Task name
var Url = 'http://example.com/Controller/Action/?jobid='+JobId ;
$.ajax({
         url:    Url ,
         success: function(result) {
                     $('input#taskname').val(result); 
                  },
         error: function(error) {
                     alert(errorss); 
                  }
    });     
});
 
Share this answer
 
v2
Comments
_Amy 29-Jul-14 1:56am    
Please format your answer before posting. :)
Gihan Liyanage 29-Jul-14 1:58am    
OK _Amy, Thank you for the advice. I will do by next
JOTHI KUMAR Member 10918227 29-Jul-14 3:28am    
sorry gihan i cant understand this kindly me briefly
a
How to bind TextBox from DropdownList in ASP NET
How to bind TextBox from DropdownList in ASP NETHow to bind TextBox from DropdownList in ASP NETHow to bind TextBox from DropdownList in ASP NETHow to bind TextBox from DropdownList in ASP NETHow to bind TextBox from DropdownList in ASP NETHow to bind TextBox from DropdownList in ASP NETHow to bind TextBox from DropdownList in ASP NETHow to bind TextBox from DropdownList in ASP NETHow to bind TextBox from DropdownList in ASP NET
How to bind TextBox from DropdownList in ASP NET
How to bind TextBox from DropdownList in ASP NET
How to bind TextBox from DropdownList in ASP NET
 
Share this answer
 
public DbSet<State> State { get; set; }

in view:-
Hide   Copy Code
@Html.DropDownList("State_Name", (IEnumerable<SelectListItem>)ViewData["ViewState"], new { id = "State_Name", @class = "form-control control-text" })

in controller:-
Hide   Copy Code
List<SelectListItem> list = new List<SelectListItem>(); 
list.Add(new SelectListItem { Text = "Select Country", Value = "Select Country" });
//set default text for DropdownList 
var cat = (from c in db.Country select c).ToArray();
//get data from database using linq 
for (int i = 0; i < cat.Length; i++) {
    list.Add(new SelectListItem { 
        Text = cat[i].Country_Name, Value = cat[i].Country_Cde.ToString() }); } ViewData["ViewCountry"] = list; 
 
Share this answer
 
Comments
[no name] 24-May-19 7:26am    
public DbSet<state> State { get; set; }

in view:-
Hide Copy Code
@Html.DropDownList("State_Name", (IEnumerable<selectlistitem>)ViewData["ViewState"], new { id = "State_Name", @class = "form-control control-text" })

in controller:-
Hide Copy Code
List<selectlistitem> list = new List<selectlistitem>();
list.Add(new SelectListItem { Text = "Select Country", Value = "Select Country" });
//set default text for DropdownList
var cat = (from c in db.Country select c).ToArray();
//get data from database using linq
for (int i = 0; i < cat.Length; i++) {
list.Add(new SelectListItem {
Text = cat[i].Country_Name, Value = cat[i].Country_Cde.ToString() }); } ViewData["ViewCountry"] = list;
[no name] 24-May-19 7:27am    
public DbSet<state> State { get; set; }

in view:-
Hide Copy Code
@Html.DropDownList("State_Name", (IEnumerable<selectlistitem>)ViewData["ViewState"], new { id = "State_Name", @class = "form-control control-text" })

in controller:-
Hide Copy Code
List<selectlistitem> list = new List<selectlistitem>();
list.Add(new SelectListItem { Text = "Select Country", Value = "Select Country" });
//set default text for DropdownList
var cat = (from c in db.Country select c).ToArray();
//get data from database using linq
for (int i = 0; i < cat.Length; i++) {
list.Add(new SelectListItem {
Text = cat[i].Country_Name, Value = cat[i].Country_Cde.ToString() }); } ViewData["ViewCountry"] = list; public DbSet<state> State { get; set; }

in view:-
Hide Copy Code
@Html.DropDownList("State_Name", (IEnumerable<selectlistitem>)ViewData["ViewState"], new { id = "State_Name", @class = "form-control control-text" })

in controller:-
Hide Copy Code
List<selectlistitem> list = new List<selectlistitem>();
list.Add(new SelectListItem { Text = "Select Country", Value = "Select Country" });
//set default text for DropdownList
var cat = (from c in db.Country select c).ToArray();
//get data from database using linq
for (int i = 0; i < cat.Length; i++) {
list.Add(new SelectListItem {
Text = cat[i].Country_Name, Value = cat[i].Country_Cde.ToString() }); } ViewData["ViewCountry"] = list; public DbSet<state> State { get; set; }

in view:-
Hide Copy Code
@Html.DropDownList("State_Name", (IEnumerable<selectlistitem>)ViewData["ViewState"], new { id = "State_Name", @class = "form-control control-text" })

in controller:-
Hide Copy Code
List<selectlistitem> list = new List<selectlistitem>();
list.Add(new SelectListItem { Text = "Select Country", Value = "Select Country" });
//set default text for DropdownList
var cat = (from c in db.Country select c).ToArray();
//get data from database using linq
for (int i = 0; i < cat.Length; i++) {
list.Add(new SelectListItem {
Text = cat[i].Country_Name, Value = cat[i].Country_Cde.ToString() }); } ViewData["ViewCountry"] = list; public DbSet<state> State { get; set; }

in view:-
Hide Copy Code
@Html.DropDownList("State_Name", (IEnumerable<selectlistitem>)ViewData["ViewState"], new { id = "State_Name", @class = "form-control control-text" })

in controller:-
Hide Copy Code
List<selectlistitem> list = new List<selectlistitem>();
list.Add(new SelectListItem { Text = "Select Country", Value = "Select Country" });
//set default text for DropdownList
var cat = (from c in db.Country select c).ToArray();
//get data from database using linq
for (int i = 0; i < cat.Length; i++) {
list.Add(new SelectListItem {
Text = cat[i].Country_Name, Value = cat[i].Country_Cde.ToString() }); } ViewData["ViewCountry"] = list; public DbSet<state> State { get; set; }

in view:-
Hide Copy Code
@Html.DropDownList("State_Name", (IEnumerable<selectlistitem>)ViewData["ViewState"], new { id = "State_Name", @class = "form-control control-text" })

in controller:-
Hide Copy Code
List<selectlistitem> list = new List<selectlistitem>();
list.Add(new SelectListItem { Text = "Select Country", Value = "Select Country" });
//set default text for DropdownList
var cat = (from c in db.Country select c).ToArray();
//get data from database using linq
for (int i = 0; i < cat.Length; i++) {
list.Add(new SelectListItem {
Text = cat[i].Country_Name, Value = cat[i].Country_Cde.ToString() }); } ViewData["ViewCountry"] = list; public DbSet<state> State { get; set; }

in view:-
Hide Copy Code
@Html.DropDownList("State_Name", (IEnumerable<selectlistitem>)ViewData["ViewState"], new { id = "State_Name", @class = "form-control control-text" })

in controller:-
Hide

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900